Java Assignment Tic-Tac-Toe For this assignment, you will be creating an interac
ID: 3817019 • Letter: J
Question
Java Assignment
Tic-Tac-Toe
For this assignment, you will be creating an interactive Tic-Tac-Toe game.
There are 2 parts to this assignment. In the first part, you are going to be given a problem and you will then need to create a structure, write algorithms and a flow chart to solve it. In the second part, you’ll be turning this into a java program.
So let’s get started!
Part 1: Your Tic-Tac-Toe game!
You love playing tic-tac-toe, but don’t always have someone to play the game with. So, you decide to create your own that you can play against the computer.
This will be a standard tic-tac-toe game with X’s and O’s where the players alternate taking turns placing their tokens (i.e., X or O, depending on which one they are). It will include:
· Standard 3 x 3 playing board (required: use 2D arrays)
· A winning game is either:
o 3 across
o 3 up and down
o 3 diagonally
· Tie games are possible if a winning game is not achieved.
· The game board with associated placement of the played X’s and O’s must be displayed after each turn.
Program Flow:
· First, ask the user for their name and welcome them.
· Then randomly decide who will go first, the user or the computer (and let the user know this)
· The user and computer will alternate turns placing their tokens.
o The user should be asked where they want to place their token
o The computer should be asked to randomly choose an empty spot
· The game ends when either a winning game occurs (see above) or all the spots are filled.
· At the end of the game, ask the user if they would like to play again.
Input Options:
There are numerous options for getting input from users to indicate where they want their token placed.
Option 1: Ask them to give you the column and row number of their desired spot.
Option 2: Pre-fill each of the spots with a number, allowing users to select one number to indicate their desired spot. For example:
1 | 2 | 3
__|___|__
4 | 5 | 6
__|___|__
7 | 8 | 9
| |
REMINDER: You must comment your code and include JavaDocs documentation as part of your assignment.
Explanation / Answer
import java.util.*;
public class ticTacToe {
enum State{Blank, X, O};
enum Res{Win,Draw,NotOver};
static int n = 3;
static State[][] board = new State[n][n];
static int moveCount;
static int turn; //odd means Computer,even means user
static Random rand = new Random();
static Scanner sc=new Scanner(System.in);
static void display(){
for (int i = 0; i<n;i++){
for (int j=0;j<n ;j++ ) {
if(board[i][j]==State.Blank){
System.out.print('_');
}else System.out.print(board[i][j]);
}
System.out.println();
}
System.out.println();
}
static void initialisiseBoard(){
// state all states as Blank
for (int i =0;i<n;i++){
for(int j =0;j<n;j++){
board[i][j]=State.Blank;
}
}
}
static Res Move(){
int x , y;
State s;
if(turn%2==0){//user
System.out.println("enter row and col number(0-indexed)");
x = sc.nextInt();
y = sc.nextInt();
s = State.X;
}else{
x = rand.nextInt(3);
y = rand.nextInt(3);
s = State.O;
}
if(board[x][y] == State.Blank){
board[x][y] = s;
display();
turn++;
moveCount++;
}else{
if(turn%2 == 0)System.out.println("Choose an empty cell");
return Res.NotOver;
}
//check end conditions
//check col
for(int i = 0; i < n; i++){
if(board[x][i] != s)
break;
if(i == n-1){
return Res.Win;
//report win for s
}
}
//check row
for(int i = 0; i < n; i++){
if(board[i][y] != s)
break;
if(i == n-1){
//report win for s
return Res.Win;
}
}
//check diag
if(x == y){
//we're on a diagonal
for(int i = 0; i < n; i++){
if(board[i][i] != s)
break;
if(i == n-1){
//report win for s
return Res.Win;
}
}
}
//check anti diag (thanks rampion)
if((x + y) == n - 1){
for(int i = 0;i<n;i++){
if(board[i][(n-1)-i] != s)
break;
if(i == n-1){
//report win for s
return Res.Win;
}
}
}
//check draw
if(moveCount == (n*n)){
//report draw
return Res.Draw;
}
return Res.NotOver;
}
public static void main(String[] args) {
System.out.println("Enter your name");
String name = sc.nextLine();
System.out.printf("Hello,%s,Welcome to Tic Tac Toe - the game. ",name);
System.out.println("Hit enter for toss");
sc.nextLine();
turn = rand.nextInt(2);//chosing player to start
moveCount = 0;
if(turn==1){
System.out.println("Computer Starts");
}else{
System.out.printf("%s, you start ",name);
}
char repeat = 'y';
while(repeat=='y'){
initialisiseBoard();
display();
while(true){
Res r = Move();
if(r == Res.Win){
if(turn%2==0){
System.out.println("You lose");
}else{
System.out.println("You won");
}
break;
}else if(r == Res.Draw){
System.out.println("Draw");
break;
}
}
System.out.println("Rematch?(y/n)");
repeat = sc.next().charAt(0);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.