// main module Module main() // Local variables Constant Integer ROWS = 3, COLS = 3 Declare Integer indexR, indexC Declare winX = 0, winO = 0, player, draw Declare String board[ROWS][COLS], LineFeed = 10 //initialize For indexR = 0 to ROWS – 1 For indexC = 0 to COLS – 1 Set board[indexR][indexC] = “*” End For End For // game While winX == 0 and winO = 0 // enter location for X Display “enter X row: “ Input player Set indexR = player - 1 Display “enter X column: “ Input player Set indexC = player – 1 If board[indexR][indexC] == “*” Then Set board[indexR][indexC] = “X” End If // enter location for O Display “enter O row: “ Input player Set indexR = player - 1 Display “enter O column: “ Input player Set indexC = player – 1 If board[indexR][indexC] == “*” Then Set board[indexR][indexC] = “O” End If // check rows For indexR = 0 to ROWS – 1 If board[indexR][0] != “*” Then If board[indexR][0] == board[indexR][1] Then If board[indexR][1] == board[indexR][2] Then If board[indexR][0] == “X” Then Set winX = 1 Else Set winO = 1 End If End If End If End If End For // check columns For indexC = 0 to COLS – 1 If board[0][indexC] != “*” Then If board[0][indexC] == board[1][indexC] Then If board[1][indexC] == board[2][indexC] Then If board[0][indexC] == “X” Then Set winX = 1 Else Set winO = 1 End If End If End If End If End For // check diagonal If board[0][0] != “*” Then If board[0][0] == board[1][1] Then If board[1][1] == board[2][2] Then If board[0][0] == “X” Then Set winX = 1 Else Set winO = 1 End If End If End If End If If board[0][2] != “*” Then If board[0][2] == board[1][1] Then If board[1][1] == board[2][0] Then If board[0][2] == “X” Then Set winX = 1 Else Set winO = 1 End If End If End If End If //check if draw Set draw = 9 For indexR = 0 to ROWS – 1 For indexC = 0 to COLS – 1 If board[indexR][indexC] == “*” Then Set draw = 0 End If End For End For If draw == 9 Then Set winX = 1 Set winO = 1 End If //display board For indexR = 0 to ROWS – 1 For indexC = 0 to COLS – 1 Display board[indexR][indexC] End For Display LineFeed End For End While // show result If draw == 9 Then Display “Game is a draw.” Else If winX == 1 Then Display “X wins!” Else Display “O wins!” End If End If End Module
This is what I have so far.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class tictactoe { // Local variables private int indexR; private int indexC; private int winX = 0; private int winO = 0; private int player; private int draw; private BufferedReader reader = new BuffferedReader (new InputStreamReader(System.in)); private char[][]board = new char [3][3]; int LineFeed = 10; //initialize protected void init(){ int counter = 0; for (indexR = 0; indexR < 3; indexR++){ for (indexC = 0; indexC < 3; indexC++){ board [indexR][indexC] = Character.forDigit(++counter, 10); } } winX = 1; player = 0; while (winX = 0; winO = 0){ //Enter location for X System.out.print ("Enter X row: "); row = reader.readLine(); indexR = player - 1; System.out.print ("Enter X column: "); column = reader.readLine(); indexC = player - 1; if (board [indexR][indexC] == "*"){ Set board[indexR][indexC] = "X"; } //enter location of O System.out.print("Enter O row"); row = reader.readLine(); indexR = player - 1; System.out.print ("Enter O column: "); column = reader.readLine(); indexC = player - 1; if (board [indexR][indexC] == "*"){ Set board[indexR][indexC] = "O"; } //check rows for (indexR = 0;indexR < 3; indexR++){ if (board[indexR][0] != "*"){ if (board[indexR][0] == board[indexR][1]){ if (board[indexR][1] == board[indexR][2]){ if (board[indexR][0] == "X"){ winX = 1; else winO = 1; } } } } } //check columns for (indexC = 0; indexC<3;indexC++){ if (board[0][indexC] != "*"){ if (board[0][indexC] == board[1][indexC]){ if (board[1][indexC] == board[2][indexC]){ if (board[0][indexC] == "X"){ winX = 1; else winO = 1; } } } } } //check diagonal if (board[0][0] != "*"){ if (board[0][0] == board[1][1]){ if (board[1][1] == board[2][2]){ if (board[0][0] == "X"){ winX = 1; else winO = 1; } } } } if (board[0][2] != "*"){ if (board[0][2] == board[1][1]){ if (board[1][1] == board[2][0]){ if (board[0][2] == "X"){ winX = 1; else winO = 1; } } } } //check if draw Set draw = 9; for (indexR = 0;indexR < 3; indexR++){ for (indexC = 0;indexC < 3; indexC++){ if (board [indexR] [indexC] == "*"){ Set draw = 0; } } } if (draw == 9){ winX = 1; winO = 1; } //display board protected string drawBoard(){ StringBuilder builder = new StringBuilder("*"); for (int indexR = 0; indexR < 3; indexR++){ for (int indexC = 0; indexC < 3; indexC++){ System.out.print ([indexR][indexC]); } System.out.print (LineFeed); } } //show result if (draw == 9){ System.out.print ("Game is a draw."); } else if (winX == 1){ System.out.print ("X wins!"); } else { System.out.print ("O wins!"); } } }