Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

initializing 2D string array (and more)

$
0
0
hello, I'm currently taking an intro to programing class and we're required to make a tic tac toe game for our final project. anyway I think I got most of the logic down but due to the fact that I have no experience with java I can't seem to get this code to compile at all. as the title above says my main concern is getting the array to initialize and print. I think once I'm able to get that much I can make a lot more progress on my own with the project. So yeah here's the code and if anyone sees anything on here that I should change or fix please please let me know.


import java.util.Scanner;

class FinalProject
{
    static string board[3][3]={{" *"," *"," *"},{" *"," *"," *"},{" *"," *"," *"}};
    public static void main()
    {
        Scanner s=new Scanner(System.in);
        {boolean gameOver= false;
          
          System.out.print("Welcome to Tic-Tac-Toe!");
          System.out.print(board[][]);
 
          While (gameOver = false){
           playerOne();
           System.out.print(board[][]);

           checkWin();

           playerTwo();
           System.out.print(board[][]);
  
           checkWin();}    
        }
    }
    public static void playerOne()
    {int rowLine;
     int colLine;
     boolean nextTurn = false;
            
     while (nextTurn=false){
            System.out.print("Player 1 [X]:");
            System.out.print("Enter row [1-3]:");
            int rowLine=s.nextInt();
            while (rowLine<1 OR rowLine>3){
              System.out.print("Invalid input, reenter choice.");
              int rowLine=s.nextInt();
            }
            System.out.println("Player 1 [X]:");
            System.out.print("Enter column [1-3]:");
            int colLine=s.nextInt();
            while (colLine<1 OR colLine>3){
              System.out.print("Invalid input, reenter choice.");
              int colLine=s.nextInt();}
     
            
            if (board[rowLine-1][co1Line-1]!= (" *")){
              System.out.print("Invalid move.");}
            else{
              board[rowLine-1][co1Line-1]= (" X");
              nextTurn= true;}
     }
    }
    
    public static void playerTwo()
    {int rowLine;
     int colLine;
     boolean nextTurn = false;
            
     while (nextTurn=false){
            System.out.print("Player 2 [O]:");
            System.out.print("Enter row [1-3]:");
            int rowLine=s.nextInt();
            while (rowLine<1 OR rowLine>3){
              System.out.print("Invalid input, reenter choice.");
              int rowLine=s.nextInt();
            }
            System.out.println("Player 2 [O]:");
            System.out.print("Enter column [1-3]:");
            int colLine=s.nextInt();
            while (colLine<1 OR colLine>3){
              System.out.print("Invalid input, reenter choice.");
              int colLine=s.nextInt();}
     
            
            if (board[rowLine-1][co1Line-1]!= (" *")){
              System.out.print("Invalid move.");}
            else{
              board[rowLine-1][co1Line-1]= (" O");
              nextTurn= true;}
     }
    }
    
    public static void checkWin()
    { int index;
      int indexA;
      int indexB;

      //Check if player 1 won
      index = 0;
      //check horizontals
      If (board[index][] = {" X"," X"," X"}){
        System.out.print("Player one wins!");
        gameOver = True;}
      else{
        if (index <=2){
        index = index+1;}
      }
      
      index = 0;
      //check verticals
      If (board[][index] = {{" X"},{" X"},{" X"}}){
        System.out.print("Player one wins!");
        gameOver = True;}
      else{
        if (index <=2){
          index = index+1;}}
 
      //Check diagonals
      If (board[0][0]=board[1][1]=board[2][2] = X OR
            board[0][2]=board[1][1]=board[2][0] = X){
             System.out.print ("Player one wins!");
              gameOver = true}
 
 
      //Check if player 2 won
      index = 0;
      //check horizontals
      If (board[index][] = {" O"," O"," O"}){
        System.out.print("Player two wins!");
        gameOver = True;}
      else{
        if (index <=2){
          index = index+1;}}

      index = 0;
      //check verticals
      If (board[][index] = {{" O"},{" O"},{" O"}}){
        System.out.print("Player two wins!");
        gameOver = True;}
      else{
        if (index <=2){
          index = index+1;}}
 
      //Check diagonals
      If (board[0][0]=board[1][1]=board[2][2] = O OR
            board[0][2]=board[1][1]=board[2][0] = O){
             System.out.print ("Player two wins!");
              gameOver = true}
 
      indexA = 0;
      indexB = 0;
      //Check if astrierisks are on the board to determine tie.
        while (board[indexA][indexB] != (" *")){
        while (indexA<= 2 AND indexB<=2){
          If (IndexA <=2){ 
            indexA = indexA+1;}
          Else{
            IndexA = 0;
              indexB = indexB+1;}
        }
        System.out.print("The game is a tie.");
        gameOver = true;

        
    }
} 



any input is much appreciated!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>