char [] row_1; char [] row_2; char [] row_3;
public class ThreeRowBoard {
public static void main (String[] args){
char [] row_1 = {'O', 'X', 'X'};
char [] row_2 = {'X', '0', 'O'};
char [] row_3 = {'X', 'O', 'O'};
System.out.println(row_1[0] + "|" + row_1[1] + "|" + row_1[2]);
System.out.println("-----");
System.out.println(row_2[0] + "|" + row_2[1] + "|" + row_2[2]);
System.out.println("-----");
System.out.println(row_3[0] + "|" + row_3[1] + "|" + row_3[2]);
}
}
$ java ThreeRowBoard
O|X|X
-----
X|0|O
-----
X|O|O
char [][] board = new char [3][3];
public class OneRowBoard {
public static void main (String[] args){
char [][] board = new char [3][3];
board[0][0] = 'O';
board[0][1] = 'X';
board[0][2] = 'X';
board[1][0] = 'X';
board[1][1] = '0';
board[1][2] = '0';
board[2][0] = 'X';
board[2][1] = '0';
board[2][2] = '0';
System.out.println(board[0][0] + "|" + board[0][1] + "|" + board[0][2]);
System.out.println("-----");
System.out.println(board[1][0] + "|" + board[1][1] + "|" + board[1][2]);
System.out.println("-----");
System.out.println(board[2][0] + "|" + board[2][1] + "|" + board[2][2]);
}
}
[1007] glenn - ~/workspace/it114/resources_it114/code_it114/examples_it114
$ java OneRowBoard
O|X|X
-----
X|0|0
-----
X|0|0
while loop
private char [][] board;
for loop
public TicTacToeBoard1 (){
board = new char [3][3];
for (int row = 0; row < 3; row++){
for (int col =0; col < 3; col++){
board[row][col] = ' ';
}
}
}
public String toString (){
String result = "";
result += row(0);
result += horizontal();
result += row(1);
result += horizontal();
result += row(2);
return result;
}
private String row(int row){
return board[row][0] + "|" + board[row][1] + "|" + board[row][2] + "\n";
}
private String horizontal (){
return "------\n";
}
public class TicTacToe {
private static TicTacToeBoard board = new TicTacToeBoard();
public static void main (String[] args){
boolean game_over = false;
TicTacToeBoard board = new TicTacToeBoard();
System.out.println(board);
}
}
$ java TicTacToe
| |
------
| |
------
| |
IllegalArgumentException
private final char USER_MARK = 'X';
private final char MACHINE_MARK = 'O';
public void mark_cell(int row, int col, char mark) throws IllegalArgumentException {
move_good(row, col, mark);
board[row][col] = mark;
}
private void move_good(int row, int col, char mark) throws IllegalArgumentException {
if (row < 0 || row > 2){
throw new IllegalArgumentException(row + " is not a valid row number");
} else if (col < 0 || col > 2) {
throw new IllegalArgumentException(col + " is not a valid column number");
} else if (mark != USER_MARK && mark != MACHINE_MARK) {
throw new IllegalArgumentException(mark + " is not a valid mark");
} else if (board[row][col] != ' ') {
throw new IllegalArgumentException("The square " + row + "," + col + " is occupied");
}
}
public class TicTacToe {
public static void main (String[] args){
TicTacToeBoard board = new TicTacToeBoard();
System.out.println(board);
board.mark_cell(1,1, 'X');
System.out.println(board);
try {
board.mark_cell(3,1,'X');
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
try {
board.mark_cell(2,3,'X');
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
try {
board.mark_cell(2,2,'Z');
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
try {
board.mark_cell(1,1,'X');
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
}
}
$ java TicTacToe
| |
------
| |
------
| |
| |
------
|X|
------
| |
3 is not a valid row number
3 is not a valid column number
Z is not a valid mark
The square 1,1 is occupied
import java.util.Scanner;
import java.util.Scanner;
public class TicTacToe {
private static TicTacToeBoard board = new TicTacToeBoard();
public static void main (String[] args){
Scanner console = new Scanner(System.in);
System.out.println(board);
user_move(console);
System.out.println(board);
}
private static void user_move(Scanner console){
boolean done = false;
while (! done){
System.out.print("Your move (row col): ");
String reply = console.nextLine();
String []fields = reply.split(" ");
int row = Integer.parseInt(fields[0]);
int col = Integer.parseInt(fields[1]);
System.out.println(row + "," + col);
try {
board.mark_cell(row, col, 'X');
done = true;
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
$ java TicTacToe
| |
------
| |
------
| |
Next move (row col): 3 3
3,3
3 is not a valid row number
Next move (row col): 2 3
2,3
3 is not a valid column number
Next move (row col): 1 1
1,1
| |
------
|X|
------
| |
// copied from https://www.techiedelight.com/generate-random-integers-specified-range-java/
public static int rand_int(int max, int min){
return new Random().nextInt(max - min + 1) + min;
}
private static void machine_move(){
boolean done = false;
while (! done){
int row = rand_int(0, 2);
int col = rand_int(0, 2);
try {
board.mark_cell(row, col, 'O');
done = true;
} catch (Exception e) {
continue;
}
}
}
machine_move();
System.out.println(board);
$ java TicTacToe.java | | ------ | | ------ | | Your move (row col): 1 1 | | ------ |X| ------ | | Machine move | | ------ |X| ------ |O|
while loop that will stop when the game is overtrue if a cell has a specific mark
private boolean cell_has_mark(int row, int col, char mark) {
return board[row][col] == mark;
}
for
that checks each column in the row
// check rows
for (int row = 0; row <= 2; row++){
if (cell_has_mark(row, 0, mark) && cell_has_mark(row, 1, mark) && cell_has_mark(row, 2, mark)){
return true;
}
}
// check columns
for (int col = 0; col <= 2; col++){
if (cell_has_mark(0, col, mark) && cell_has_mark(1, col, mark) && cell_has_mark(2, col, mark)){
return true;
}
}
// check diagonals
if (cell_has_mark(0, 0, mark) && cell_has_mark(1, 1, mark) && cell_has_mark(2, 2, mark)){
return true;
} else if (cell_has_mark(0, 2, mark) && cell_has_mark(1, 1, mark) && cell_has_mark(2, 0, mark)) {
return true;
}
false$ java TicTacToe | | ------ | | ------ | | Next move (row col): 1 1 | | ------ |X| ------ | | |O| ------ |X| ------ | | Next move (row col): 0 1 The square 0,1 is occupied Next move (row col): 1 0 |O| ------ X|X| ------ | | |O| ------ X|X| ------ | | Next move (row col): 1 2 |O| ------ X|X|X ------ | | User wins! $ java TicTacToe | | ------ | | ------ | | Next move (row col): 1 1 | | ------ |X| ------ | | O| | ------ |X| ------ | | Next move (row col): 0 1 O|X| ------ |X| ------ | | O|X| ------ O|X| ------ | | Next move (row col): 2 1 O|X| ------ O|X| ------ |X| User wins! $ java TicTacToe.java | | ------ | | ------ | | Next move (row col): 1 1 | | ------ |X| ------ | | |O| ------ |X| ------ | | Next move (row col): 0 0 X|O| ------ |X| ------ | | X|O| ------ O|X| ------ | | Next move (row col): 2 2 X|O| ------ O|X| ------ | |X User wins!
for loop that loop over each row and columnfalsefor loop finish without finding a space, we return true
public boolean game_is_tie() {
for (int row = 0; row <= 2; row++){
for (int col = 1; col <= 2; col++){
if (board[row][col] == ' ') {
return false;
}
}
}
return true;
}
public static void main (String[] args){
Scanner console = new Scanner(System.in);
System.out.println(board);
while (true){
user_move(console);
System.out.println(board);
if (board.player_wins('X')){
System.out.println("User wins!");
break;
}
if (board.game_is_tie()) {
System.out.println("Game is over. No one wins.");
break;
}
machine_move();
System.out.println(board);
if (board.player_wins('O')){
System.out.println("Machine wins");
break;
}
}
}