Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is a list of the specifications for your submission. Read the entire list and decide which items should be considered first. Don't try and

Here is a list of the specifications for your submission. Read the entire list and decide which items should be considered first. Don't try and finish the entire assignment in one sitting. Programming is an incremental process. Do one thing at a time (starting early) and you will that progress is being made.

You must use the Scanner class for user input (from the keyboard).

You must use a 2-dimensional array of type Character to represent the game grid.

You can use String and StringBuilder objects as you need.

You should not use any other classes in your code. There will be some other classes for reading the init file, but you won't be coding with these yourself in this assignment. You can use always the Math class if you wish.

Your main method will drive your game by calling other static methods in your class. The overall control flow of your game will be in main but the details of the different actions will be in other static methods.

Particular tasks should have their own method. (This is procedural programming!)

If zero command line arguments are given then your game will be between one human player and the computer. The human player will play first and they will play X. The computer will initially be O and play second.

If one command line argument is given and it is the string 2p, then the game will be between two human players. Player 1 will start with X (and player 2 will be O). Do not ask the players for their names. They are simply player 1 and player 2.

A game ends when either a player wins or all positions are played and there is no winner (a draw). You do not need to recognize a draw until the entire grid is filled. (A better version of the game would determine if at any point in the game is must end in a draw. You are not responsible for this.)

When a game ends, a user friendly message will say which player won the game (player 1 or player 2 or computer) and how many symbols in a row they won with. This number might be greater than K (if K is smaller then N and/or M).

Your program will let a player (or players) play as many games as they wish. After the user friendly message is displayed when a game ends the user (or users) is asked if they would like to player another game or end. The answer to this question (text input) will be yes or no (in lower case only).

When a new game is started, the player that previously started the game will now play second and the player that previously stated second will start the game. The starting player will always play the symbol X, but their name(denoted player 1 or player 2 or computer) will remain fixed. In each subsequent game, the player that starts will change (alternate).

The game grid will be displayed (showing all X's, O's and blanks) after each move is played (by either a human player or the computer). When the computer plays a move, a line of text stating its move should be be displayed before the grid is shown.

Your game must be user friendly. That is, it must provide useful prompts to the user during the game.

You can assume that all input text is valid. That is, we will only test your code with input that follows this specification.

If a user enters a position to play that is not allowed, because there is already a symbol in that position or the position does not exist in the grid, a useful message is displayed to the screen and they are asked to enter another position. This repeats until a position is played that is currently empty in the grid. An example of a position that does not exists is trying to play in position r 12 c 4 in 10-10-10 game.

When the user(s) decide to stop playing, after completing a game, some statistics will be displayed. You will output the total number of games played, the total number of wins by each player, the total number of draws, and the largest winning row/column/diagonal of any game played. For example, the output might look like

Thanks for playing Total games : 7 Player 1 wins : 2 Computer wins : 3 Number of draws : 2 Best win : 6 in a row 

There will be no other input asked for or expected from the user during the game.

Your game must play a valid game of tic-tac-toe++.

Remember to push your changes back to your master repository on GitHib (so that your assignment can be graded).

Example

A run of a human versus computer 2-4-3 game might look like the following

C:\user> java TicTacToeGame Welcome to tic-tac-toe++ player 1 input : r 0 c 0 X | | | ---+---+---+--- | | | computer plays r 1 c 1 X | | | ---+---+---+--- | O | | player 1 input : c 4 row 0 That move is not allowed. Try again. player 1 input : c 1 row 0 X | X | | ---+---+---+--- | O | | computer plays r 0 C 3 X | X | | O ---+---+---+--- | O | | player 1 input : c 2 roW 0 X | X | X | O ---+---+---+--- | O | | Player 1 wins with 3 symbols! Would you like to play again? no Thanks for playing tic-tac-toe++ total games : 1 player 1 wins : 1 computer wins : 0 draws : 0 best win : 3 symbols 

The code is the bellow just complete it:

import java.util.Scanner;
import java.io.FileReader;
import java.io.BufferedReader;
public class TicTacToeGame{
/** symbol for X */
public static final Character ex = 'X';
/** symbol for O */
public static final Character oh = 'O';
/** symbol for empty grid element */
public static final Character empty = ' ';
/** board is the grid that the game is played on;
* each element must be one of ex, oh or empty*/
public static Character[][] board;
/** rows is the number of rows (N) in the game board */
public static int rows;
/** columns is the number of columns (M) in the game board */
public static int columns;
/** win_condition is the value of K, the winning condition of the game */
public static int win_condition;
/** specifies if the game is 1p (human-human) or 2p (human-computer) */
public static boolean human_human_game;
/** Checks for a win based on the last symbol played in the game
*
* It is assumed that the position specified by the last_row_played
* and last_column_played is valid and that the symbol in the board
* at that position is not empty. (It must be ex or oh)
*
* @param last_row_played is the row of the last symbol played
* @param last_column_played is the column of the last symbol played
* @return the length of the winning row/column/diagonal of symbols
* if the last move is a winning move, or -1 if the last move is not
* a winning move.
*/
public static int win(int last_row_played, int last_column_played){
// add your code and change the return value
return -2;
}
/** main method to run the game
*
* @param args optional command line arguments to
* specify if the game is 1p (human-computer) or
* 2p (human-human). Expect the string "2p" if any
* command line argument is given.
*/
public static void main(String[] args){
/*------------------------------------------
* handle command line arguments if any
* to determine if the game is human-human
* or human-computer
*------------------------------------------*/
if( args.length > 0){
/* there are commend line arguments present */
// add your code here
}else{
/* there are no command line argument present */
// add your code here
}
/*------------------------------------
* read N-M-K data from init file
* N = rows
* M = columns
* K = win_condition
*------------------------------------*/
/*-------------------------------------
*-------------------------------------
* BEGIN : Do NOT change the code here
*-------------------------------------*/
BufferedReader file_input;
FileReader file;
String file_name = "init";
String line;
try{
file = new FileReader(file_name);
file_input = new BufferedReader(file);
line = file_input.readLine();
rows = Integer.parseInt(line);
line = file_input.readLine();
columns = Integer.parseInt(line);
line = file_input.readLine();
win_condition = Integer.parseInt(line);
/* always close your files you are done with them! */
file_input.close();
}catch(Exception e){
/* somethine went wrong! */
System.err.println("Failure to read data from init file properly");
System.err.println(e);
System.err.println("Program ending");
return;
}
/*-------------------------------------
* END : Do NOT change the code here
*-------------------------------------
*-------------------------------------*/
/* create and initialize the game board */
/* allocate memory for the board array */
board = new Character[rows][columns];
// add code to initialize all elements to empty
/* code to drive the game */
// add your code here
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions