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!
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...
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?
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
Perk Company produces three products: Tic, Tac, and Toe. Tic requires 90 machine setups, Tac requires...
Perk Company produces three products: Tic, Tac, and Toe. Tic requires 90 machine setups, Tac requires 80 setups, and Toe requires 330 setups. Berk has identified an activity cost pool with allocated overhead of $18,000 for which the cost driver is machine setups. How much overhead is assigned to the Tic product?
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their moves on the same computer. Create a 3-by-3 two-dimensional array. Each player indicates their moves by entering a pair of numbers representing the row and column indices of the square in which they want to place their mark, either an 'X' or an 'O'. When the first player moves, place an 'X' in the specified square. When the second...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT