Let\'s use what you\'ve learned about drawing methods to create a simple tic-tac
ID: 3815652 • Letter: L
Question
Let's use what you've learned about drawing methods to create a simple tic-tac-toe game.
The game rules are simple:
- players take turns marking cells in a 3 x 3 grid with either X's or O's
- As soon as all cells in a row, column, or diagonal are marked by the same player, that player wins and the game is over.
So your task is to:
create a graphic window with a 3 x 3 grid,
allow players to mark an empty cell by clicking within it
place an X or O in the center of the selected cell (you can assume players alternate turns)
when a player has won (according to the above rules), draw a line through the winning row, column, or diagonal cells
Hint:
You can use a counter variable and a while loop to get mouse clicks until all cells are filled or a player wins,
You can compare the x & y coordinates of each mouse click against coords of grid lines to determine the row and column selected. Because cells are equal size, you know the x coordinate for each vertical line, and the y coordinate for each horizonal line.
You'll need some data structure to track which cells have been marked and by which player. A 2-dimensional list may be most convenient, since it can match the rows and columns of your game. The below code creates a 3x3 list pre-filled with -1 for each item, to indicate the item hasn't been selected yet:
Items in a 2-dimensional list can be accessed by index, just like 1-d list items:
If you don't have the textbook, a full list of graphics.py methods is available at:
https://github.com/MrAlex6204/Books/blob/master/Python%20Programming-An%20Introduction%20to%20Computer%20Science%202nd%20edition-John%20Zelle%202010.pdf (Links to an external site.)
Please upload .py file or use word, pdf ...
Explanation / Answer
import java.util.Scanner;
public class {
static public boolean isPlayer1Turn = true;
static int mRow = 0;
static int mColumn = 0;
public static void main(String[] args) {
boolean keepPlaying = true;
boolean isClear;
Scanner input = new Scanner(System.in);
String[][] grid = createGrid(3, 3);
while (keepPlaying) {
displayGrid(grid);
do {
mRow = askForRow();
mColumn = askForColumn();
isClear = isPositionClear(grid);
if (!isClear) {
System.out.println("row: " + mRow + " column: " + mRow + " is already been chosen. Try again.");
}
} while (!isClear);
updateTurn(grid);
if (hasWon(grid)) {
displayGrid(grid);
String player = (isPlayer1Turn) ? "X" : "O";
System.out.println("Player " + player + " has won.");
System.out.print("Do you want to play again? y/n: ");
String option = input.next();
keepPlaying = (option.equalsIgnoreCase("y"));
grid = createGrid(3, 3);
}
isPlayer1Turn = !isPlayer1Turn;
}
System.out.println("Game ended..");
input.close();
}
public static int askForColumn(){
Scanner input = new Scanner(System.in);
int column = 0;
boolean isColumnValid = false;
while (!isColumnValid) {
if (isPlayer1Turn) {
System.out.print("Enter a column (0, 1, or 2) for player X: ");
} else {
System.out.print("Enter a column (0, 1, or 2) for player O: ");
}
column = input.nextInt();
isColumnValid = isValidRange(column);
}
return column;
}
public static int askForRow(){
int row = 0;
Scanner input = new Scanner(System.in);
boolean isRowValid = false;
while (!isRowValid) {
if (isPlayer1Turn) {
System.out.print("Enter a row (0, 1, or 2) for player X: ");
} else {
System.out.print("Enter a row (0, 1, or 2) for player O: ");
}
row = input.nextInt();
isRowValid = isValidRange(row);
}
return row;
}
public static boolean isValidRange(int section) {
return (0 <= section && section <= 2);
}
public static boolean isPositionClear(String[][] grid) {
String position = grid[mRow][mColumn];
return position.equals("| |") || position.equals(" |");
}
public static void displayGrid(String[][] grid) {
for (int i = 0; i < grid.length; i++) {
System.out.println("--");
for (int k = 0; k < grid[i].length; k++) {
System.out.print(grid[i][k]);
}
System.out.println("");
}
System.out.println("---");
}
public static String[][] createGrid(int row, int column) {
String[][] grid = new String[row][column];
for (int i = 0; i < grid.length; i++) {
for (int k = 0; k < grid[i].length; k++) {
if (k == 0)
grid[i][k] = "| |";
else
grid[i][k] = " |";
}
}
return grid;
}
public static boolean updateTurn(String[][] grid) {
if (!grid[mRow][mColumn].equals("| |") &&
!grid[mRow][mColumn].equals(" |")) return false;
String update;
if (isPlayer1Turn)
update = (mColumn == 0) ? "| X |" : " X |";
else
update = (mColumn == 0) ? "| O |" : " O |";
grid[mRow][mColumn] = update;
return true;
}
public static boolean hasWon(String[][] grid) {
int player = (isPlayer1Turn) ? 0 : 1;
String token = (player == 0) ? "X" : "O";
return (checkColumn(grid, token) || checkColumn(grid,token) || checkDiagonal(grid, token));
}
public static boolean checkColumn(String[][] grid, String s) {
int occurence = 0;
for (int k = 0; k < grid[0].length; k++) {
for (int i = 0; i < grid.length; i++) {
if (grid[i][k].contains(s)) occurence++;
}
if (occurence == 3) return true;
else occurence = 0;
}
return false;
}
public static boolean checkRow(String[][] grid, String s) {
int occurrence = 0;
for (int i = 0; i < grid.length; i++) {
for (int k = 0; k < grid[i].length; k++) {
if (grid[i][k].contains(s)) occurrence++;
}
if (occurrence == 3) return true;
else occurrence = 0;
}
return false;
}
public static boolean checkDiagonal(String[][] grid, String s) {
int occurrence = 0;
int x = 0;
int y = 0;
while (x < grid[0].length && y < grid.length) {
if (grid[y][x].contains(s)) occurrence++;
if (occurrence == 3) return true;
x++;
y++;
}
x = 0;
y = grid.length - 1;
while (x < grid[0].length && y >= 0) {
if (grid[y][x].contains(s)) occurrence++;
if (occurrence == 3) return true;
y--;
x++;
}
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.