How do I check if a player has inputted the same coordinates twice?

26 views Asked by At

I'm making a game in which two players (playerX and playerO) take turns selecting coordinates to place an 'X' or a 'O', like Tik-Tak-Toe.

import java.util.*;

public class Methods
{
    //Scanner in = new Scanner(System.in);
    // responsible for input
    
    // checks if the move is valid(if not it makes the player input moves until it is valid)
    public static  void moveValid(int row ,int col,char[][] board)
    {
        Scanner in = new Scanner(System.in);
        // checks if a postion is taken
        boolean taken = false;
        for(int r = 0; r < board.length;r++)
        {
            for(int c = 0;c < board[row].length;c++)
            {
                if((row ==0)&& (col==0))
                {
                    if((board[0][1] == 'O') || (board[0][1] =='X'))
                    {
                        taken = true;
                    }
                }
            }
        }
        
        if(((col >2 ) || (row > 2))|| taken)
        {
            boolean right = false;
            while(right == false)
            {       
                System.out.println("Invalid move, enter a new move.\n");
                System.out.println("X enter the colum for your move (0-2):");
                int newCol = in.nextInt();
                System.out.println("X enter the row for your move(0-2):");
                int newRow = in.nextInt();           
                System.out.println("");
                                
                if(((newCol <= 2) && (newRow <= 2)) || taken == false )
                {
                    right = true;
                }
            }
        }
    }
    
    // positioning of the pieces
    public static void change(char[][] board,char player, int row, int col)
    {
        if((row ==0) && (col ==0))
        {
            board[0][1] = player;
        }
        if((row ==0) && (col ==1))
        {
            board[0][5] = player;
        }
        if((row ==0) && (col ==2))
        {
            board[0][9] = player;
        }
        if((row ==1) && (col ==0))
        {
            board[2][1] = player;
        }
        if((row ==1) && (col ==1))
        {
            board[2][5] = player;
        }
        if((row ==1) && (col ==2))
        {
            board[2][9] = player;
        }
        if((row ==2) && (col ==0))
        {
            board[4][1] = player;
        }
        if((row ==2) && (col ==1))
        {
            board[4][5] = player;
        }
        if((row ==2) && (col ==2))
        {
            board[4][9] = player;
        }
    }
}

That class is supposed to check if the moves are valid and, if so, changes the array accordingly.

import java.util.*;

public class Main
{

    public static void main(String[] args)  
    {   
        Scanner kb = new Scanner(System.in);
        // creating the board and pieces
        char [][] array = 
        {
            {' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
            {'-','-','-','-','-','-','-','-','-','-','-'},
            {' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
            {'-','-','-','-','-','-','-','-','-','-','-'},
            {' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '}
        };

        char playerX = 'X';
        char playerO ='O';

        // positions 
        char posOne = array[0][1];
        char posTwo = array[0][5];
        char posThree = array[0][9];

        char posFour = array[1][1];
        char posFive = array[1][5];
        char posSix = array[1][9];

        char posSeven = array[2][1];
        char posEight = array[2][5];
        char posNine = array[2][9];

        boolean done = false;

        for(int row =0; row < array.length;row++)
        {
            System.out.println(array[row]);
        }
        System.out.println("");

        // first move

        System.out.println("X enter the column for your move (0-2):");
        int columnLocation = kb.nextInt();
        System.out.println("X enter the row for your move (0-2):");
        int rowLocation = kb.nextInt();
        System.out.println("");


        /*
        // if the first move is invalid
            if((columnLocation >2 ) || (rowLocation > 2))
            {
                boolean right = false;
                    while(right == false)
                    {
                        System.out.println("Invalid move, enter a new move.\n");
                        System.out.println("X enter the colum for your move (0-2):");
                        columnLocation = kb.nextInt();
                        System.out.println("X enter the row for your move(0-2):");
                        rowLocation = kb.nextInt();
                        System.out.println("");
                            if((columnLocation <= 2) && (rowLocation <= 2))
                            {
                                right = true;
                            }
                    }
            }

            */

        Methods.moveValid(rowLocation,columnLocation,array);
        Methods.change(array,playerX,rowLocation,columnLocation);

        for(int row =0; row < array.length;row++)
        {
            System.out.println(array[row]);
        }
        System.out.println("");


        // first O

        System.out.println("X enter the column for your move (0-2):");
        columnLocation = kb.nextInt();
        System.out.println("X enter the row for your move (0-2):");
         rowLocation = kb.nextInt();
        System.out.println("");

        if((columnLocation >2 ) || (rowLocation > 2))
            {
                boolean right = false;
                    while(right == false)
                    {
                        System.out.println("Invalid move, enter a new move.\n");
                        System.out.println("X enter the colum for your move (0-2):");
                        columnLocation = kb.nextInt();
                        System.out.println("X enter the row for your move(0-2):");
                        rowLocation = kb.nextInt();
                        System.out.println("");
                            if((columnLocation <= 2) && (rowLocation <= 2))
                              {
                                right = true;
                              }
                       }
               }
           Methods.change(array,playerO,rowLocation,columnLocation);
            for(int row =0; row < array.length;row++)
           {
                System.out.println(array[row]);
        }
        System.out.println("");
    }
}

This class is the main method. I'm testing the playerX first, then I'm testing playerO next. The array changes correctly for the first time for the cooridantes (0,0), but when I enter (0,0) for playerO, the array changes to replace 'X' with 'O'. How do I make it so a player can not move to a spot another player is on?

0

There are 0 answers