import javautilScanner import javautilRandom public class PlayTTT public static void main String args Scanner reader new ScannerSystemin ID: 370116
Download Presentation The PPT/PDF document "TIC TAC TOE" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
TIC TAC TOESlide2
import
java.util.Scanner;import java.util.Random; public class PlayTTT{ public static void main(String[]args){ Scanner reader = new Scanner(System.in); TTTBoard board = new TTTBoard(); System.out.println(board);
-
-
-
-
-
-
-
-
-Slide3
// Randomly decide who goes
first
Random
gen =
new
Random
();
String
letter
= "X";
if
(
gen.nextInt
(2) == 1)
letter
= "O";Slide4
while (true){ String
winner
=
board.
getWinner
();
if
(
winner
!=
null
){
System.
out.println
(
winner
+ "s
win
!");
break;
}
System.
out.println
(
letter
+ "'s
turn
");
System.
out.print
("
Enter
the
row
[1-3]: ");
int
row
=
reader.nextInt
();
System.
out.print
("
Enter
the
column
[1-3]: ");
int
column
=
reader.nextInt
();
boolean
success =
board.placeXorO
(letter, row, column);
if
(
success
){
System.
out.println
(
board
);
//
Switch
the
player
if
(
letter
== "X")
letter
= "O";
else
letter
= "X";
}
else
System.
out.println
("Error: cell already occupied!");
}Slide5
public
class TTTBoard{
private
String[][] board; public TTTBoard(){ board = new String[3][3]; reset(); }
CONSTRUCTOR METHOD FOR THE BOARDSlide6
toString
method
public
String toString(){ String result = ""; for (int row = 0; row < board.length; row++){ for (int column = 0; column < board[0].length; column++) result
+=
board
[
row
][
column
] + " ";
result
+= "\n";
}
return
result
;
}Slide7
getWinner
method
public
String getWinner(){ if (winner("X")) return "X"; else if (winner("O")) return "O";
else
return
null
;
}Slide8
Winner
method (private)
private
boolean winner(String s){ // Create a target string for the search String test = s + s + s; // Create an array to hold the possible strings int rows = board.length; int
columns
=
board
[0].
length
;
String
possibles
[]
=
new String[rows + columns + 2];
// Get the three columns as strings
for (
int
column = 0; column < columns; column ++)
possibles
[
column
] =
getColumn
(
column
,
rows
);
// Get the three rows as strings
for (
int
row = 0; row < rows; row ++)
possibles
[
columns
+
row
] =
getRow
(
row
,
columns
);
// Get the two diagonals as strings
possibles
[
possibles.length
- 2] =
getDiagonal
(
0, rows, columns);
possibles
[
possibles.length
- 1] =
getDiagonal
(rows - 1, rows, columns);
// Search for the target string
for
(
int
i = 0; i <
possibles.length
; i++)
if
(
test.equals
(
possibles
[i]))
return
true;
return
false;
}Slide9
getColumn
() method (private)
private String
getColumn(int column, int rows){ String result = ""; for (int row = 0; row < rows; row++) result += board[row][column];
return
result
;
}Slide10
getRow
() method (private)
private String
getRow
(int row, int columns){ String result = ""; for (int column = 0; column < columns; column++) result += board[row][column];
return
result
;
}Slide11
getDiagonal
() method (private)
private String
getDiagonal
(int startRow, int rows, int columns){ String result = ""; int column = 0; if (startRow == 0)
while
(
startRow
<
rows
){
result
+=
board
[
startRow
][
column
];
startRow
++;
column
++;
}
else
while
(
startRow
>= 0){
result
+=
board
[
startRow
][
column
];
startRow
--;
column
++;
}
return
result
;
}Slide12
r
eset() method (public)
public
void reset(){ for (int row = 0; row < board.length; row++) for (int column = 0; column < board[0].length; column++) board[row][column] = "-";
}
Slide13
placeXorO
() method (public)
public
boolean placeXorO(String s, int row, int column){ if (board[row - 1][column - 1].equals("-")){ board[row - 1][column - 1] = s; return true; } else return false; }
Slide14
test
XXXSlide15
possible[] =
012
3
4
5
67
X0X
XX0
00X
XX0
XXX
,
,
,Slide16
possible[] =
0
1
2
3
4
5
6
7
X
0
X
0
x
X
X
0
0
X
0 X
0 X
0Slide17
0
123
4
5
6
7
X
0
X
0
x
X
X
0
0
X
0 X
0 X
0
X
X
X
X 0 X
0 X
X
X 0 0
possible[] =