arbertictactoe

 avatar
unknown
java
5 years ago
6.4 kB
7
Indexable
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class TicTacToe
{
    //stores player1 positions for use in checkWinner
    static ArrayList<Integer> playerPositions = new ArrayList<Integer>();
    //stores player2 positions
    static ArrayList<Integer> player2Positions = new ArrayList<Integer>();

    public static void main(String[] args) {
        char[][] gameBoard = {{' ', '|', ' ', '|', ' '},
                              {'-', '+', '-', '+', '-'},
                              {' ', '|', ' ', '|', ' '},
                              {'-', '+', '-', '+', '-'},
                              {' ', '|', ' ', '|', ' '}};

        printBoard(gameBoard);
        int turn = 0; //turn increases
        String currentPlayer = "player"; //currentPlayer switches when turn switches
        int playerNum = 1; //playerNum switches between 1 and 2 based on turns

        while (playerPositions.size() + player2Positions.size() <= 9)
        {
            if (turn == 9)
            {
                System.out.println("CAT!");
                break;
            }
            if (turn % 2 == 0)
            {
                currentPlayer = "player";
                playerNum = 1;

            }
            else
            {
                currentPlayer = "player2";
                playerNum = 2;
            }

            Scanner scan = new Scanner(System.in);
            System.out.println("Enter your placement player " + playerNum + " (1 - 9):");
            int playerPos = scan.nextInt(); //player1 position
            while (playerPositions.contains(playerPos) || player2Positions.contains(playerPos))
            {
                System.out.println("Position taken, enter a correct position (1 - 9):");
                playerPos = scan.nextInt();
            }
            while (playerPos > 9 || playerPos < 1)
            {
                System.out.println("Enter number in range (1 - 9):");
                playerPos = scan.nextInt();
            }
            if (turn % 2 == 0)
            {
                currentPlayer = "player";
                playerNum = 1;

            }
            else
            {
                currentPlayer = "player2";
                playerNum = 2;
            }
            placePiece(gameBoard, playerPos, currentPlayer);
            printBoard(gameBoard); //printboard after player 1 has went

            int result = checkWinner(); //checking the winner
            if (result > 0)
            {
                System.out.println(stringWinner(result));
                break;
            }
            turn++;
        }

    }

    public static void printBoard(char[][] gameBoard) //prints the board
    {
        for (char[] row : gameBoard) {
            for (char c : row) {
                System.out.print(c);
            }
            System.out.println();
        }
    }

    //places a piece
    public static void placePiece(char[][] gameBoard, int pos, String user)
    {
        char symbol = ' ';

        if (user.equals("player"))
        {
            symbol = 'X';
            playerPositions.add(pos);
        } else if (user.equals("player2"))
        {
            symbol = 'O';
            player2Positions.add(pos);
        }

        switch (pos) { //switch case to place a piece according to symbol
            case 1:
                gameBoard[0][0] = symbol;
                break;
            case 2:
                gameBoard[0][2] = symbol;
                break;
            case 3:
                gameBoard[0][4] = symbol;
                break;
            case 4:
                gameBoard[2][0] = symbol;
                break;
            case 5:
                gameBoard[2][2] = symbol;
                break;
            case 6:
                gameBoard[2][4] = symbol;
                break;
            case 7:
                gameBoard[4][0] = symbol;
                break;
            case 8:
                gameBoard[4][2] = symbol;
                break;
            case 9:
                gameBoard[4][4] = symbol;
                break;
            default:
                break;
        }
    }

    //checks winner
    //@return: int result
    public static int checkWinner()
    {
        int result = 0;

        List topRow = Arrays.asList(1, 2, 3);
        List midRow = Arrays.asList(4, 5, 6);
        List botRow = Arrays.asList(7, 8, 9);
        List leftCol = Arrays.asList(1, 4, 7);
        List midCol = Arrays.asList(2, 5, 8);
        List rightCol = Arrays.asList(3, 6, 9);
        List cross1 = Arrays.asList(1, 5, 9);
        List cross2 = Arrays.asList(3, 5, 7);

        List<List> winning = new ArrayList<List>();
        winning.add(topRow);
        winning.add(midRow);
        winning.add(botRow);
        winning.add(leftCol);
        winning.add(midCol);
        winning.add(rightCol);
        winning.add(cross1);
        winning.add(cross2);

        List draw = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        List<List> draw1 = new ArrayList<List>();

        //goes through the lists in winning and checks
        //if playerPositions or player2Positions contain all the numbers
        //in the lists
        //if both positions add up to 9 result = 3 (CAT)
        for (List l : winning)
        {
            if (playerPositions.containsAll(l))
            {
                return 1;
            }
            if (player2Positions.containsAll(l))
            {
                return 2;
            }
            if (playerPositions.size() + player2Positions.size() > 9)
            {
                return 3;
            }

        }
        return 0;
    }

    //converts int result into a String
    public static String stringWinner(int result) //takes the int result to a string
    {
        String string = "";
        if (result == 1)
        {
            string = "Congrats, Player 1 WON!";
        }
        else if (result == 2)
        {
            string = "Congrats, Player 2 WON!";
        }
        else if (result == 3)
        {
            string = "CAT!";
        }
        return string;
    }

}
Editor is loading...