Pls I need this program .
It's my project.
Write a blue j program for tic tac Toe with using scanner and buffered reader and without using main class and private and public. Keep it as simple as possible.
Answers
Answered by
0
So I have a basic code for running a tic-tac-toe board. I'm supposed to fill in the missing pieces, but I've having a lot of problems. I'm supposed to use double nested arrays I believe.
Requirements:
As soon as one player gets three in a row, the game should end.
If the board fills up, the program should print that there was a tie, and the game should end.
If the user enters a row/column that is outside the board (less than 0 or more than 2), print an error and let them enter another spot. Similarly, if they enter row/column and another piece is already in that spot, print and error and let them enter another position.
I mostly need some pointers, and any code help would be appreciated. When I've run the program, it just keeps looping through the Player X inputs and won't stop or show the board. What's in bold is what I have added to it.
JAVA CODE:
public class TicTacToe {
2
3 private char[][] board;
4 private char currentPlayerMark;
5
6 public TicTacToe() {
7 board = new char[3][3];
8 currentPlayerMark = 'x';
9 initializeBoard();
10 }
11
12
13 // Set/Reset the board back to all empty values.
14 public void initializeBoard() {
15
16 // Loop through rows
17 for (int
40
41
42 // Loop through all cells of the board and if one is found to be empty (contains char '-') then return false.
43 // Otherwise the board is full.
44 public boolean isBoardFull() {
45 boolean isFull = true;
46
47 for (int i = 0; i < 3; i++) {
48 for (int j = 0; j < 3; j++) {
49 if (board[i][j] == '-') {
50 isFull = false;
51 }
52 }
53 }
54
55 return
76
77 // Loop through columns and see if any are winners.
78 private boolean checkColumnsForWin() {
79 for (int i = 0; i < 3; i++) {
80 if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) {
81 return true;
82 }
83 }
84 return false;
85 }
86
87
88 // Check the two diagonals to see if either is a win. Return true if either wins.
89 private boolean checkDiagonalsForWin() {
90 return ((checkRowCol(board, board
Requirements:
As soon as one player gets three in a row, the game should end.
If the board fills up, the program should print that there was a tie, and the game should end.
If the user enters a row/column that is outside the board (less than 0 or more than 2), print an error and let them enter another spot. Similarly, if they enter row/column and another piece is already in that spot, print and error and let them enter another position.
I mostly need some pointers, and any code help would be appreciated. When I've run the program, it just keeps looping through the Player X inputs and won't stop or show the board. What's in bold is what I have added to it.
JAVA CODE:
public class TicTacToe {
2
3 private char[][] board;
4 private char currentPlayerMark;
5
6 public TicTacToe() {
7 board = new char[3][3];
8 currentPlayerMark = 'x';
9 initializeBoard();
10 }
11
12
13 // Set/Reset the board back to all empty values.
14 public void initializeBoard() {
15
16 // Loop through rows
17 for (int
40
41
42 // Loop through all cells of the board and if one is found to be empty (contains char '-') then return false.
43 // Otherwise the board is full.
44 public boolean isBoardFull() {
45 boolean isFull = true;
46
47 for (int i = 0; i < 3; i++) {
48 for (int j = 0; j < 3; j++) {
49 if (board[i][j] == '-') {
50 isFull = false;
51 }
52 }
53 }
54
55 return
76
77 // Loop through columns and see if any are winners.
78 private boolean checkColumnsForWin() {
79 for (int i = 0; i < 3; i++) {
80 if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) {
81 return true;
82 }
83 }
84 return false;
85 }
86
87
88 // Check the two diagonals to see if either is a win. Return true if either wins.
89 private boolean checkDiagonalsForWin() {
90 return ((checkRowCol(board, board
Ananyavijetha:
pls read the question properly.dont use public znd private
Answered by
0
// Check minor diagonal lineFormed = true; for (int i = 1; i < board.length; i++) if (board[board.length - i][i - 1] != number || board[board.length - i - 1][i] != number) lineFormed = false; if (lineFormed) return true; return false; // If none of the lines is formed } }
Sample run:
| | | | | | | | | | | | Enter a row (1, 2 or 3) for player X: 1 3Enter a column (1, 2 or 3) for player X: | | |X| | | | | | | | | Enter a row (1, 2 or 3) for player O: 1 Enter a column (1, 2 or 3) for player O: 2 | |O|X| | | | | | | | | Enter a row (1, 2 or 3) for player X: 2 Enter a column (1, 2 or 3) for player X: 2 | |O|X| | |X| | | | | | Enter a row (1, 2 or 3) for player O: 3 Enter a column (1, 2 or 3) for player O: 3 | |O|X| | |X| | | | |O| Enter a row (1, 2 or 3) for player X: 3 Enter a column (1, 2 or 3) for player X: 1 | |O|X| | |X| | |X| |O| X player won Process finished with exit code 0
Similar questions