Question

In: Computer Science

Develop a very simple Tic Tac Toe gaming software in Java. Ifyou want, you may...

Develop a very simple Tic Tac Toe gaming software in Java. If you want, you may take some code (with proper reference to the source from where it is taken - i.e., hackerrank or github) from an existing game implementation to extend/improve the functionalities. To develop a good quality software, you would like to follow a particular software engineering process or more than one process (waterfall, incremental, agile etc.) and choose an appropriate programming language.

Solutions

Expert Solution

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

/**
*
* @author MeneXia (Xavi Ablaza)
*
*/
public class TicTacToe {
   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.");
       System.out.println("--------------------------------");
       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;
           }
       }
       if (winner.equalsIgnoreCase("draw")) {
           System.out.println("It's a draw! Thanks for playing.");
       } else {
           System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing.");
       }
   }

   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;
   }

   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);
       }
   }
}


Related Solutions

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
If you were to write a game of tic-tac-toe, you may store the representation of the...
If you were to write a game of tic-tac-toe, you may store the representation of the game board as a two dimensional list such as   [['X', '', 'X'], ['O', 'X', ''], ['', 'O', 'X']] where each sublist is a row in the board.   Empty strings ('') denote a space that does not yet have a value. Assuming this representation, write functions (contained in the same file) that do the following: a) Create a new empty tic-tac-toe board. This function should...
I am having an issue with the Java program with a tic tac toe. it isn't...
I am having an issue with the Java program with a tic tac toe. it isn't a game. user puts in the array and it prints out this. 1. Write a method print that will take a two dimensional character array as input, and print the content of the array. This two dimensional character array represents a Tic Tac Toe game. 2. Write a main method that creates several arrays and calls the print method. Below is an example of...
Can someone design a Tic Tac Toe game in java in the simplest way you can?...
Can someone design a Tic Tac Toe game in java in the simplest way you can? It should have -the markers for the players are X and O -2 players -instructions on how to play the game in the start of the game -show the Tic Tac Toe grid before asking each player for their input -be on one java file Thank you!!!!!!! This is for an intro class so please don't make it too complicated!
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking 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 draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the...
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the following restrictions (SO WRITE TWO FILES THAT PLAY PYTHON THROUGH A SOCKET) Use a 5 x 5 grid (dimensions are subject to change, so use constants for NUM_ROWS and NUM_COLS) Use 'X' for player 1 and 'O' for player 2 (symbols and the number of players is subject to change, so use constants) Each player can make 1 move per turn before having to...
Tic-Tac-Toe, also called X's and O's, Noughts and Crosses, and X and 0 is a simple...
Tic-Tac-Toe, also called X's and O's, Noughts and Crosses, and X and 0 is a simple game played on a 3x3 grid, referred to as the board. Lines may be horizontal, vertical, or diagonal. You will implement a Board class to represent the 3x3 grid. This class will have functions to determine which symbol, if any, is in a cell, to place a symbol in a cell, to determine the winner, if any so far, and to print the board...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
Implement the Tic-tac-toe game for variable board sizes, you may assume: 2 < s < 11,...
Implement the Tic-tac-toe game for variable board sizes, you may assume: 2 < s < 11, where s is the board size. Before the game starts the program will prompt for the board size. Note: the winning conditions are the same as the original Tic-tac-toe game in that you need to fill the entire row/column/diagonal to win. Here are a few sample runs. The output is a bit different so that we can handle two-digit coordinates consistently. We expect (but...
In a game of tic tac toe. How do you check if all the positions in...
In a game of tic tac toe. How do you check if all the positions in a double array are taken, the double arrays are used to store the x's and o's?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT