Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am doing a project on tictactoe game using java without using arrays or anything else, but listing the win combinations. Although I'm done that, this next part is confusing me and I don't know how to start it. Could you start me up with few lines of code and how to properly execute it?

I am required to this: Option #3 sets every location on the board to 'b' and then inputs the location of players' moves until the letter 'Q' is entered. Then, the board is displayed.

Note: The program should assume that x always goes first

Here's an example of Option #3...
--- Input ---	--- Expected output---
3
TL
TR
BR
Q	                 xbo
                         bbb
                         bbx
3
TM
MM
BM
TL
BL
Q

                         oxb
                         bob
                         xxb



I will also paste my code and whatever I have so far, and option 3 starts all the way below I have set a boolean and a while statement but idk what to put inside that:


Java
import java.util.Scanner;
public class tictactoe{
    public static void main(String[]args){
        Scanner keyboard = new Scanner(System.in);
            int option = keyboard.nextInt();
            String TL = keyboard.next();
            String TM = keyboard.next();
            String TR = keyboard.next();
            String ML = keyboard.next();
            String MM = keyboard.next();
            String MR = keyboard.next();
            String BL = keyboard.next();
            String BM = keyboard.next();
            String BR = keyboard.next();
                if (option == 1){
                        System.out.print(TL);
                        System.out.print(TM);
                        System.out.print(TR);
                        System.out.print("\n"+ML);
                        System.out.print(MM);
                        System.out.print(MR);
                        System.out.print("\n"+BL);
                        System.out.print(BM);
                        System.out.print(BR);
            }
                else if (option == 2){
                        System.out.print(TL);
                        System.out.print(TM);
                        System.out.print(TR);
                        System.out.print("\n"+ML);
                        System.out.print(MM);
                        System.out.print(MR);
                        System.out.print("\n"+BL);
                        System.out.print(BM);
                        System.out.print(BR);
                            if (TL.equals(TM) && TL.equals(TR) && TL.equals(TR) && !TL.equals("b")){
                                System.out.println("\n"+TL +" "+"wins");
            }
                            else if (ML.equals(MM) && ML.equals(MR) && !ML.equals("b")){
                                System.out.println("\n"+ML +" "+"wins");
            }
                            else if (TL.equals(ML) && TL.equals(BL) && !TL.equals("b")){
                                System.out.println("\n"+TL +" "+"wins");
            }
                            else if (BL.equals(BM) && BL.equals(BR) && !BL.equals("b")){
                                System.out.println("\n"+BM +" "+"wins");
            }
                            else if (TM.equals(MM) && TM.equals(BM) && !TM.equals("b")){
                                System.out.println("\n"+TM +" "+"wins");
            }
                            else if (TR.equals(MM) && TR.equals(BL) && !TR.equals("b")){
                                System.out.println("\n"+TR +" "+"wins");
            }
                            else if (TR.equals(MR) && TR.equals(BR) && !TR.equals("b")){
                                System.out.println("\n"+TR +" "+"wins");
            }
                            else if (TL.equals(MM) && TL.equals(BR) && !TL.equals("b")){
                                System.out.println("\n"+TL +" "+"wins");
            }
                            else{
                                System.out.println("\n"+"No Winner");
            }
                        
            }
                else if (option == 3){
                            TL = "b";
                            TM = "b";
                            TR = "b";
                            ML = "b";
                            MM = "b";
                            MR = "b";
                            BL = "b";
                            BM = "b";
                            BR = "b";
                            boolean go = true;
                                while (go){
                                    
            }
        }
    }
}


What I have tried:

The above code is what i tried
Posted
Updated 12-Apr-20 11:50am
v3
Comments
Richard MacCutchan 13-Apr-20 4:28am    
Your code would make more sense, and option 3 would be easier, if you used a two-dimensional array for your board, rather than hard coded names for each location. You can then iterate through each row and column quite quickly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900