Question

Hi, I am a student in college who is trying to make a tic tac toe...

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 own adjustments, which is what I did, I just need to make my own adjustments later. I just need some help or maybe even suggestions on what exactly my tic tac toe game code should be like. I'm sorry for being a bit stupid and I know this question may sound complicated, but I really need all the help I can get. I failed one class last semester and I do not plan on failing another class this semester. So please help....I really need it. Thank you very much. My code is right below.

import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class TicTacToe implements Game {

public boolean playGame(Player player) {

static Scanner in;
static String[] board;
static String turn;

public static void main(String[] args) {

  in = new Scanner(System.in);
  board = new String[9];
  turn = "X";

  String winner = null;
  populateEmptyBoard();

  System.out.println("Welcome to 2 Player Tic Tac Toe.");

  printBoard();

  System.out.println("X's will play first. Enter a slot number to place X in:");

  while (winner == null) {
   int numInput;

   try {
    numInput = in.nextInt();
   if (!(numInput > 0 && numInput <= 9)) {
     System.out.println("Invalid input; re-enter slot number:");
     continue;
    }

   } catch (InputMismatchException e) {
    System.out.println("Invalid input; re-enter slot number:");
    continue;
   }

   if (board[numInput-1].equals(String.valueOf(numInput))) {
    board[numInput-1] = turn;

    if (turn.equals("X")) {
     turn = "O";

    } else {
     turn = "X";
    }

    printBoard();
    winner = checkWinner();

   } else {

    System.out.println("Slot already taken; re-enter slot number:");
    continue;
   }

  }

  //game will end with a draw
  if (winner.equalsIgnoreCase("draw")) {
   System.out.println("It's a draw! Thanks for playing.");

  //game will end when there is a winner
  } else {
   System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing.");
  }

}


//all of the options available for when a player aligns 3 X's or O's
static String checkWinner() {
  for (int a = 0; a < 8; a++) {
   String line = null;

   switch (a) {
   case 0:

    line = board[0] + board[1] + board[2];

    break;

   case 1:

    line = board[3] + board[4] + board[5];

    break;

   case 2:

    line = board[6] + board[7] + board[8];

    break;

   case 3:

    line = board[0] + board[3] + board[6];

    break;

   case 4:

    line = board[1] + board[4] + board[7];

    break;

   case 5:

    line = board[2] + board[5] + board[8];

    break;

   case 6:

    line = board[0] + board[4] + board[8];

    break;

   case 7:

    line = board[2] + board[4] + board[6];

    break;
   }

   if (line.equals("XXX")) {
    return "X";

   } else if (line.equals("OOO")) {
    return "O";
   }

  }

  for (int a = 0; a < 9; a++) {
   if (Arrays.asList(board).contains(String.valueOf(a+1))) {
    break;
   }
   else if (a == 8) return "draw";
  }

  System.out.println(turn + "'s turn; enter a slot number to place " + turn + " in:");
  return null;
}


//making the board
static void printBoard() {
  System.out.println("/---|---|---\\");

  System.out.println("| " + board[0] + " | " + board[1] + " | " + board[2] + " |");

  System.out.println("|-----------|");

  System.out.println("| " + board[3] + " | " + board[4] + " | " + board[5] + " |");

  System.out.println("|-----------|");

  System.out.println("| " + board[6] + " | " + board[7] + " | " + board[8] + " |");

  System.out.println("/---|---|---\\");
}

static void populateEmptyBoard() {
  for (int a = 0; a < 9; a++) {
   board[a] = String.valueOf(a+1);
  }

}

public String getName() {
  return "A game of Tic-Tac-Toe";
  }
}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Hi, I am a student in college who is trying to make a tic tac toe...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    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...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    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...

    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 traditional Tic Tac Toe game, two players take turns to mark grids with noughts and...

    In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    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...

  • You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people....

    You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...

  • PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1....

    PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1. Create the data structure – Nine slots that can each contain an X, an O, or a blank. – To represent the board with a dictionary, you can assign each slot a string-value key. – String values in the key-value pair to represent what’s in each slot on the board: ■ 'X' ■ 'O' ■ ‘ ‘ 2. Create a function to print the...

  • Python Complete the player_turn() function that completes a single turn of the game Tic-Tac-Toe (also called...

    Python Complete the player_turn() function that completes a single turn of the game Tic-Tac-Toe (also called Noughts and Crosses). The function takes 3 parameters: 1. The first parameter is called board. It is a list of length 3, where each entry is a string with 3 characters representing a row on a Tic-Tac-Toe board. Each character in a row represents a slot on the Tic-Tac-Toe board with the "#" character indicating an empty slot. An empty board looks like this:...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an...

    (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a 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 draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT