JAVA Only
Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with.
import java.util.Scanner;
public class TicTacToe {
private final int
BOARDSIZE = 3; // size of the board
private enum Status {
WIN, DRAW,
CONTINUE }; // game states
private char[][]
board; // board representation
private boolean
firstPlayer; // whether it's player 1's
move
private boolean
gameOver; // whether game is over
// Constructor that builds the game
public TicTacToe() {
// Constructor that
builds the game
/*****************************************
* Student Provide the
code*
*******************************************/
}
// start game
public void play() {
Scanner input = new
Scanner(System.in);
int row; // row
for next move
int column;
// column for next move
System.out.println("Player X's
turn.");
while
(!gameOver) {
// start
game
/*****************************************
*
Student Provide the code*
*******************************************/
}
}
// show game status in status bar
private void
printStatus(int player) {
Status status = gameStatus();
// check game status
switch
(status) {
case WIN:
System.out.printf("Player %c
wins.", player);
gameOver = true;
break;
case DRAW:
System.out.println("Game is a
draw.");
gameOver = true;
break;
case CONTINUE:
if (player == 'X')
System.out.println("Player O's
turn.");
else
System.out.println("Player X's
turn.");
break;
}
}
// get game status
private Status gameStatus()
{
int a;
// check for a win on
diagonals
// check for win in rows
// check for win in
columns
// check for a completed
game
/*****************************************
* Student Provide the
code*
*******************************************/
}
// display board
public void printBoard() {
System.out.println("
_______________________ ");
for
(int row = 0; row < BOARDSIZE;
row++) {
System.out.println("|
|
| |");
for (int column = 0; column <
BOARDSIZE; column++) {
printSymbol(column, board[row][column]);
}
System.out.println("|_______|_______|_______|");
}
}
// print moves
private void
printSymbol(int column, char
value) {
System.out.printf("|
%c ", value);
if (column == 2)
{
System.out.println("|");
}
}
// validate move
private boolean
validMove(int row, int column)
{
return row >= 0
&& row < 3 && column >= 0 && column
< 3 &&
board[row][column] == 0;
}
}
----------------------------Code to Test --------------------------------
// Play a game of Tic Tac Toe
public class TicTacToeTest {
public static void
main(String[] args) {
TicTacToe game =
new TicTacToe();
game.printBoard();
game.play();
}
}
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thanks.
TicTacToeTest.java
package classess6;
import java.util.Scanner;
class TicTacToe {
private final int BOARDSIZE = 3; // size of the
board
private enum Status { WIN, DRAW, CONTINUE }; // game
states
private char[][] board; // board representation
private boolean firstPlayer; // whether it's player
1's move
private boolean gameOver; // whether game is over
// Constructor that builds the game
public TicTacToe() {
// Constructor that builds the
game
/*****************************************
* Student Provide the code*
*******************************************/
board = new
char[BOARDSIZE][BOARDSIZE];
// Loop through rows
for (int i = 0; i < BOARDSIZE;
i++) {
// Loop
through columns
for (int j = 0;
j < BOARDSIZE; j++) {
board[i][j] = '-';
}
}
}
// start game
public void play() {
Scanner input = new
Scanner(System.in);
int row; // row for next move
int column; // column for next
move
firstPlayer = true;
System.out.println("Player X's
turn.");
while (!gameOver) {
row =
input.nextInt();
column =
input.nextInt();
while(!validMove(row,column)){
System.out.println("Invalid move, Please try
again...");
row = input.nextInt();
column = input.nextInt();
}
if(firstPlayer)
board[row][column] = 'X';
else
board[row][column] = 'O';
printBoard();
if(firstPlayer)
printStatus('X');
else
printStatus('O');
firstPlayer =
!firstPlayer;
// start
game
/*****************************************
* Student
Provide the code*
*******************************************/
}
}
private boolean checkRowCol(char c1, char c2, char c3)
{
return ((c1 != '-') && (c1
== c2) && (c2 == c3));
}
// show game status in status bar
private void printStatus(int player) {
Status status = gameStatus();
// check game status
switch (status) {
case WIN:
System.out.printf("Player %c wins.", player);
gameOver =
true;
break;
case DRAW:
System.out.println("Game is a draw.");
gameOver =
true;
break;
case CONTINUE:
if (player ==
'X')
System.out.println("Player O's turn.");
else
System.out.println("Player X's turn.");
break;
}
}
// Loop through rows and see if any are winners.
private boolean checkRowsForWin() {
for (int i = 0; i < BOARDSIZE;
i++) {
if
(checkRowCol(board[i][0], board[i][1], board[i][2]) == true)
{
return true;
}
}
return false;
}
// Loop through columns and see if any are
winners.
private boolean checkColumnsForWin() {
for (int i = 0; i < BOARDSIZE;
i++) {
if
(checkRowCol(board[0][i], board[1][i], board[2][i]) == true)
{
return true;
}
}
return false;
}
// Check the two diagonals to see if either is a
win. Return true if either wins.
private boolean checkDiagonalsForWin() {
return ((checkRowCol(board[0][0],
board[1][1], board[2][2]) == true) || (checkRowCol(board[0][2],
board[1][1], board[2][0]) == true));
}
// get game status
private Status gameStatus() {
int a;
boolean isFull = true;
for (int i = 0; i <
BOARDSIZE; i++) {
for (int j = 0;
j < BOARDSIZE; j++) {
if (board[i][j] == '-') {
isFull = false;
}
}
}
if(isFull){
return
Status.DRAW;
}
// check for a win on
diagonals
if(checkRowsForWin()){
return
Status.WIN;
}else
if(checkColumnsForWin()){
return
Status.WIN;
}else
if(checkDiagonalsForWin()){
return
Status.WIN;
}
return Status.CONTINUE;
}
// display board
public void printBoard() {
System.out.println("
_______________________ ");
for (int row = 0; row <
BOARDSIZE; row++) {
System.out.println("| | | |");
for (int
column = 0; column < BOARDSIZE; column++) {
printSymbol(column, board[row][column]);
}
System.out.println("|_______|_______|_______|");
}
}
// print moves
private void printSymbol(int column, char value)
{
System.out.printf("| %c ",
value);
if (column == 2) {
System.out.println("|");
}
}
// validate move
private boolean validMove(int row, int column) {
return row >= 0 && row
< BOARDSIZE && column >= 0 && column <
BOARDSIZE &&
board[row][column] == '-';
}
}
// Play a game of Tic Tac Toe
public class TicTacToeTest {
public static void main(String[] args) {
TicTacToe game = new
TicTacToe();
game.printBoard();
game.play();
}
}
JAVA Only Help on the sections that say Student provide code. The student Provide code has...
This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe { private char currentPlayer; private char[][] board; private char winner; /** * Default constructor * Initializes the board to be 3 by 3 and...
Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending). Please ensure the resulting code completes EACH part of the following prompt: "Your task is to create a game of Tic-Tac-Toe using a 2-Dimensional String array as the game board. Start by creating a Board class that holds the array. The constructor should use a traditional for-loop to fill the array with "blank" Strings (eg. "-"). You may want to include other instance data......
This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The program will assign X to Player 1, and O to Player The program will ask Player 1, to...
Code in JAVA You are asked to implement “Connect 4” which is a two player game. The game will be demonstrated in class. The game is played on a 6x7 grid, and is similar to tic-tac-toe, except that instead of getting three in a row, you must get four in a row. To take your turn, you choose a column, and slide a checker of your color into the column that you choose. The checker drops into the slot, falling...
Please I need your help I have the code below but I need to add one thing, after player choose "No" to not play the game again, Add a HELP button or page that displays your name, the rules of the game and the month/day/year when you finished the implementation. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class TicTacToe extends JFrame implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board...
Hi, I am a student in college who is trying to make a tic tac toe game on Java. Basically, we have to make our own game, even ones that already exist, on Java. We also have to implement an interface, that I have no idea exactly. The professor also stated we need at least 2 different data structures and algorithms for the code. The professor said we could use code online, we'd just have to make some of our...
Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Project3 extends JFrame implements ActionListener { private static int winxpos = 0, winypos = 0; // place window here private JButton exitButton, hitButton, stayButton, dealButton, newGameButton; private CardList theDeck = null; private JPanel northPanel; private MyPanel centerPanel; private static JFrame myFrame = null; private CardList playerHand = new CardList(0); private CardList dealerHand = new CardList(0);...