Tic- Tac- Toe

NOTE: There are no TABS on the codes. THESE ARE MY PERSONAL solutions to the problem. Copy at your own risk.

#10. Tic-Tac-Toe Game

REQUIREMENT
Design a program that allows two players to play a game of tic-tac-toe. Use a 2-dimensional String array with 3 rows and 3 columns as the game board. Each element in array should be initialized with an asterisk(*). The program should run a loop that does the following:

Display the contents of the board array
Allows Player 1 to select a location on the bard for an X. The program should ask the user to enter the row and column number.
Allows Player 2 to select a location on the bard for an O. The program should ask the user to enter the row and column number.
Determines if a player won or a tie occurred.
If a players wins the program declares the winner and exists.
If a tie occurs the program declares result and exits.
Player 1 wins if there are 3 consecutive Xs.
Player 2 wins if there are 3 consecutive Os.
The winning Xs or Os can appear in row, column or diagonally.
A tie occurs when all of the locations on the board are full, but there is no winner.

tic-tac-toe

PSEUDO CODE
//CONSTANTS
Constant String tileB = ” _ ”
Constant String tileX = ” X ”
Constant String tileO = ” O ”
Constant Integer WIN = 3
Constant Integer idxMAX = 2
Constant Integer tileMAX = 9
Constant String LBR = “\n”

//OTHER PROPERTIES
Declare Boolean Integer DRAWN = 0 // 0 is false, 1 is true
Declare Boolean Integer isOver = 0 // 0 is false, 1 is true
Declare String cturn = tileX
Declare Integer SIZE = 3
Declare String board = [ [], [], [] ]

//—————–
//Display Functions
//—————–

Function showMsg(msg)
Declare String Msg
Display Msg
End Function

Function showErr(msg)
Declare String errorPrefix = “ERROR: ”
Display errorPrefix, msg
End Function

Function showBoard(pboard)
Declare String output = “”;
For row = 0 To SIZE -1
For col = 0 To SIZE -1
Set output = output, pboard[row][col] //draw tile
End For
// draw new row
Set output = output, LBR
End For
Display showMsg(output)
End Function

//——————–
//Validation Functions
//——————–

Function outOfBounds(row, column)
If row < 0 OR row > idxMAX Then
Display showErr(“Out of Bounds”)
Return 1
Else If column < 0 OR column > idxMAX Then
Display showErr(“Out of Bounds”)
Return 1
Else
Return 0
End If
End Function

Function occupied(pboard, row, column)
If pboard[row][column] != tileB Then
Display showErr(“Slot is occupied.”)
Return 1
Else
Return 0
End If
End Function

//————–
//Game Functions
//————–

Function takeTurn(pturn)
If pturn == tileX Then
Return tileO
Else
Return tileX
End If
End Function

Function move(player, pboard)
//take player input
Declare Integer moveRow = 0
Declare Integer moveCol = 0
Do
Display “Please enter Row for “, player, “”
Input moveRow
Display “Please enter Column”, player, “”
Input moveCol
While outOfBounds(moveRow, moveCol) == 1 OR occupied(pboard, moveRow, moveCol) == 1
Set pboard[moveRow][moveCol] = player
Call showBoard(pboard)
Return takeTurn(player)
End Function

//—————
//State Functions
//—————

//Check game state
Function isGameOver(pboard)
Declare Integer rc = 0
Declare Integer i = 0,j = 0

//checkRow
For i = 0 To SIZE -1
If pboard[i][0] == tileX AND pboard[i][1] == tileX AND pboard[i][2] == tileX Then
Display “X wins”
Set rc = 1
Else If pboard[i][0] == tileO AND pboard[i][1] == tileO AND pboard[i][2] == tileO Then
Display “O wins”
Set rc = 1
End If
End For

//check column
For j = 0 To SIZE -1
If pboard[0][j] == tileX AND pboard[1][j] == tileX AND pboard[2][j] == tileX Then
Display “X wins”
Set rc = 1
Else If pboard[0][j] == tileO AND pboard[1][j] == tileO AND pboard[2][j] == tileO Then
Display “O wins”
Set rc = 1
End If
End For

//check across
If pboard[1][1] == tileX Then
If pboard[0][0] == tileX AND pboard[2][2] == tileX Then
Display “X wins”
Set rc = 1
Else If pboard[0][2] == tileX AND pboard[2][0]==tileX
Display “X wins”
Set rc = 1
End If
Else If pboard[1][1] == tileO Then
If pboard[0][0] == tileO AND pboard[2][2] == tileO Then
Display “O wins”
Set rc = 1
Else If pboard[0][2] == tileO AND pboard[2][0]==tileO
Display “O wins”
Set rc = 1
End If
End If
Return rc
End Function

//————–
/*MAIN PROGRAM*/
//————–

//generate board
Declare Integer row = 0
Declare Integer col = 0

For row = 0 To SIZE -1
For col = 0 To SIZE -1
Set board[row][col] = tileB
End For
End For

Call showBoard(board)

//make moves
Declare Integer moveNum = 0;

Do
Set cturn = move(cturn, board)
Set moveNum = moveNum + 1
While moveNum < tileMAX AND isGameOver(board) == 0 Display "GAME OVER!"

Leave a Reply

Your email address will not be published. Required fields are marked *