Skip to content

Tic Tac Toe written in Swift - Do you think you can win? Give it a try!

Notifications You must be signed in to change notification settings

215eight/TicTacToeSwift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TicTacToeSwift

Tic Tac Toe written in Swift - Do you think you can win? Give it a try!

Concepts

  • Symbol: A nought (O) or a cross (X)

  • Position: A zero-based numeric value that indicates the relative location of a given position within a delimited space with an specific layout

  • Board: A rectangular array of N x N positions. The first position is located at the top left corner of the board. Position increments its value horizontally until a row ends. The next position is the right-most location in the row below. Below an example of a 3 x 3 grid

  0 | 1 | 2
----+---+----
  3 | 4 | 5 
----+---+----
  6 | 7 | 8

Each position in the board has a value equal to a symbol. All positions start with an empty value. Its value can only be changed once.

  • Position Names: Each position within a board has a name depending its realtive location.

    • Edge: A position located on the edge that is not at the begining of a row or column
    • Corner: A position bordered by two edge positions
    • Center: A position located not on the edge
 corner |  edge  | corner
--------+--------+-------
  edge  | center |  edge
--------+--------+-------
 corner |  edge  | corner
  • Units: N number of contiguous positions horizontally, vertically or diagonally aligned. A board has a total of 2N + 2 nunber of units. N number of rows + N number of columns + 2 diagonals

Horizontal Unit

  X |   |       
----+---+----
  X |   |       
----+---+----
  X |   |       

Vertical Unit

  X | X | X     
----+---+----
    |   |       
----+---+----
    |   |       

Diagonal Unit

  X |   |       
----+---+----
    | X |       
----+---+----
    |   | X     
  • Units For Position: Each position within a board belongs to a certain number of units depending its location. A unit with that has the same symbol in all its positions, is called a winning unit

    • Position 0: Belongs to a horizontal unit (first row), a vertical unit (first column) and a diagonal unit
    • Position 3: Belongs to a horizontal unit and a vertical unit, but not a diagonal unit
    • Position 4: Belogns to a horizontal unit, a vertical unit and two diagonals
  • Winning Units For Position: In the initial state of a board where all positions are empty, the chances of each position to become part of a winning unit is equal to the number of units it belongs to. For each symbol, the number of winning units for each position starts reducing every time a position is taken with an opposite symbol. The number of winning units for a taken position by an opposite symbol is equal to 0. Also, the positions in all the units for a taken position are reduced by one. Below a graphical explanation

O Symbol Winning Units - Initial State

    |   |        3 | 2 | 3     
----+---+----  ----+---+----   
    |   |        2 | 4 | 2     
----+---+----  ----+---+----   
    |   |        3 | 2 | 3     

O Symbol Winning Units - Position 4 = X

    |   |        2 | 1 | 2     
----+---+----  ----+---+----   
    | X |        1 | 0 | 1     
----+---+----  ----+---+----   
    |   |        2 | 1 | 2     

O Symbol Winning Units - Position 0 = X

  X |   |        0 | 0 | 1     
----+---+----  ----+---+----   
    | X |        0 | 0 | 1     
----+---+----  ----+---+----   
    |   |        1 | 1 | 2     

Operations

  • Set Symbol at Postion: This is the basic operation of the game. The value of a given position is modified only if its current value is empty. If this is true, then the number of winning units for all positions in the board are updated for each symbol.

  • Block With Symbol: Identifies units with N-1 positions all filled with the opposite of the given symbol as value. Select one of the units that meets the aforementioned criteria and set the empty position with the given symbol.

  • Win With Symbol: Identifies units with N-1 positions all filled with the given symbol as value. Select one of the unit that meets the aforementioned criteria and set the empty position with the given symbol.

Strategy For A Perfect Game

One of the constraints in this exercise is that the computer never loses. In other words, it always wins or, at best, draws.

About

Tic Tac Toe written in Swift - Do you think you can win? Give it a try!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages