Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this example the objective is to create a tic-tac-toe game implementation, wh

ID: 3746347 • Letter: I

Question

In this example the objective is to create a tic-tac-toe game implementation, which has been started however there are several pieces that are missing or incomplete. I need help implementing the remaining lines of java code to make the game work. So essentially, what code is it missing and where does it go?

import java.util.Scanner;
import java.io.*;
/**
*/
public class Main {

    // Our keyboard scanner
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        boolean nextGame = true;
        while (nextGame) {
            // Play one game
            oneGame();

            // Check if we want to continue
            System.out.print("Shall we play one more (y/n)? ");
            String next = in.nextLine();
            if ((next.charAt(0) == 'y') || (next.charAt(0) == 'Y'))
                nextGame = true;
            else
                nextGame = false;
        }
        System.out.println("Goodbye!");
    }


    public static void oneGame() {
        char[][] playField = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}} ;

        // Game loop
        while (true) {
            printBoard(playField);
            getUserInput(playField);

            if (findWin('X',playField)) {
                System.out.println("Congratulations, you won!");
                break;
            }
            if (findTie(playField)) {
                System.out.println("It's a draw!");
                break;
            }

            getComputerInput(playField);
            if (findWin('O',playField)) {
                System.out.println("Better luck next time!");
                break;
            }
            if (findTie(playField)) {
                System.out.println("It's a draw!");
                break;
            }

            // Next round, show where we are
            printBoard(playField);
        }
        printBoard(playField);
    }

    public static void getUserInput(char[][] playField) {
        boolean valid = false;

        while (!valid) {
            System.out.print("Please enter your move (row and col, no space): ");
            String move = in.nextLine();
            if (move.length() == 2) {
                int row = (int) move.charAt(0) - 97;
                int col = (int) move.charAt(1) - 49;

                if (((row == 0) || (row == 1) || (row == 2))
                        && ((col == 0) || (col == 1) || (col == 2))) {
                    if (playField[row][col] == 0 || playField[row][col] == ' ') {
                        playField[row][col] = 'X';
                        valid = true;
                    } else {
                        System.out.println("This field is already taken, choose another!");
                    }
                } else {
                    System.out.println("This field is invalid!");
                }
            } else {
                System.out.println("Please only enter the right code (a1,...) for the play!");
            }
        }
    }

    public static void getComputerInput(char[][] playField) {
        // Computer move
    }

    public static boolean findTie(char[][] playField) {
// Determine TIE condition
    }

    public static boolean findWin(char playerChar, char[][] playField) {
        int winSum = playField.length * (int) playerChar;
        int sum = 0;
        boolean gameOver = false;
        // Across
        for (int i = 0; i < playField.length; i++) {
            sum = 0;
            for (int j = 0; j < playField[i].length; j++) {
                sum += (int) playField[i][j];
            }
            if (sum == winSum)
                gameOver = true;
        }

        // Diagonal (top left, low right)
        sum = 0;
        for (int i = 0, j = 0; i < playField.length && j < playField[0].length; i++, j++) {
            sum += (int) playField[i][j];
        }
        if (sum == winSum)
            gameOver = true;

        return gameOver;
    }

    public static void printBoard(char[][] playField) {
        // Print the board
        System.out.println("     1     2     3");
        System.out.println(" +-----+-----+-----+");
        System.out.println("a | " + playField[0][0] + " | " + playField[0][1] + " | "
                + playField[0][2] + " |");
        System.out.println(" |-----+-----+-----|");
        System.out.println("b | " + playField[1][0] + " | " + playField[1][1] + " | "
                + playField[1][2] + " |");
        System.out.println(" |-----+-----+-----|");
        System.out.println("c | " + playField[2][0] + " | " + playField[2][1] + " | "
                + playField[2][2] + " |");
        System.out.println(" +-----+-----+-----+");

    }

}

Explanation / Answer

Here is the code for the Tic Tac Toe game which is perfectly in working condition.

Please review the code and if you have any doubts or any changes needed please comment below. I wil help you. Thank you.

import java.util.Scanner;

public class main {
  
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
char[][] TTT_Board = new char[3][3];

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
TTT_Board[i][j] = '_';
}
}
int count_moves = 0;
char player_turn = 'X';

System.out.println("Current TTT_Board: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(TTT_Board[i][j]);
System.out.print(" ");
}
System.out.println();
}
System.out.println();
  
while (count_moves < 9) {
  
boolean Valid_Input = false;
  
while(!Valid_Input){
System.out.print("Enter row (0-2): ");
int row = s.nextInt();
System.out.print("Enter column (0-2): ");
int column = s.nextInt();
if(row>=0 && row<=2 && column>=0 && column<=2){
if(TTT_Board[row][column]=='_'){
TTT_Board[row][column] = player_turn;
Valid_Input = true;
}
else{
System.out.println("Position already filled, enter again");
}
}
else{
System.out.println("Invalid Position, enter again");
}
}
  
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(TTT_Board[i][j]);
System.out.print(" ");
}
System.out.println();
}
System.out.println();
  
  
boolean win = false;
for (int i = 0; i < 3; i++) {
if (TTT_Board[i][0] == TTT_Board[i][1] &&
TTT_Board[i][1] == TTT_Board[i][2] &&
TTT_Board[i][0] == player_turn) win = true;

  
if (TTT_Board[0][i] == TTT_Board[1][i] &&
TTT_Board[1][i] == TTT_Board[2][i] &&
TTT_Board[0][i] == player_turn) win = true;
}

  
if(TTT_Board[0][0]==TTT_Board[1][1] &&
TTT_Board[1][1]==TTT_Board[2][2] &&
TTT_Board[2][2]== player_turn) win = true;
  
if(TTT_Board[0][2]==TTT_Board[1][1] &&
TTT_Board[1][1]==TTT_Board[2][0] &&
TTT_Board[2][0]== player_turn) win = true;
  
if (win) {
System.out.println(player_turn + " wins!");

  
break;
}

  
if(player_turn=='X')
player_turn = 'O';
else if(player_turn=='O')
player_turn = 'X';
count_moves++;
}

if (count_moves == 9) System.out.println("Tie game.");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote