Wednesday, 2 September 2020

Tic-tac-toe in Java

  • This is a functional tic-tac-toe game in Java.
  • All things will be explained when you run the code.
  • Type the following code in your IDE

import java.util.Scanner;



public class tic_tac_toe {


public static void main(String[] args) {

String turn="";

Scanner scan=new Scanner(System.in);

System.out.println("Welcome to tic-tac-toe");

System.out.println("Let's start");

System.out.println("Take a sheet of paper and draw a 3-by-3 grid");

System.out.println("Now mark them like this:");

System.out.println("1 2 3");

System.out.println("4 5 6");

System.out.println("7 8 9");

System.out.println("You can tell me your turn by writing only the cell number(1-9), nothing else.");

System.out.println("Only legal play is allowed. You get error in wrong turns.");

for(;;){

loop:{

System.out.println("\nComputer:X in 7");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("1")){

System.out.println("Computer:X in 9");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("8")){

System.out.println("Computer:X in 3");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("5")){System.out.println("Computer:X in 6");System.out.println("Computer won.");break loop;}

if (turn.equals("6")){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

if (turn!="5" & turn!="6"){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

}

if (turn!="8"){System.out.println("Computer:X in 8");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("2")){

System.out.println("Computer:X in 1");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("4")){

System.out.println("Computer:X in 9");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("5")){System.out.println("Computer:X in 8");System.out.println("Computer won.");break loop;}

if (turn.equals("8")){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

if (turn!="5" & turn!="8"){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

}

if (turn!="4"){System.out.println("Computer:X in 4");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("3")){

System.out.println("Computer:X in 1");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("4")){

System.out.println("Computer:X in 9");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("5")){System.out.println("Computer:X in 8");System.out.println("Computer won.");break loop;}

if (turn.equals("8")){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

if (turn!="5" & turn!="8"){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

}

if (turn!="4"){System.out.println("Computer:X in 4");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("4")){

System.out.println("Computer:X in 9");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("8")){

System.out.println("Computer:X in 3");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("5")){System.out.println("Computer:X in 6");System.out.println("Computer won.");break loop;}

if (turn.equals("6")){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

if (turn!="5" & turn!="6"){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

}

if (turn!="8"){System.out.println("Computer:X in 8");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("5")){

System.out.println("Computer:X in 1");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("4")){

System.out.println("Computer:X in 6");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("3")){System.out.println("Computer:X in 9");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("2")){System.out.println("Computer:X in 8");System.out.println("Computer won.");break loop;}

if (turn.equals("8")){System.out.println("Computer:X in 2");System.out.println("Match draw.");break loop;}

}

if (turn.equals("9")){System.out.println("Computer:X in 3");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("2")){System.out.println("Computer:X in 8");System.out.println("Match draw.");break loop;}

if (turn.equals("8")){System.out.println("Computer:X in 2");System.out.println("Computer won.");break loop;}

}

if (turn.equals("2")){System.out.println("Computer:X in 8");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("3")){System.out.println("Computer:X in 9");System.out.println("Computer won.");break loop;}

if (turn.equals("9")){System.out.println("Computer:X in 3");System.out.println("Match draw.");break loop;}

}

if (turn.equals("8")){System.out.println("Computer:X in 2");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("3")){System.out.println("Computer:X in 9");System.out.println("Match draw.");break loop;}

if (turn.equals("9")){System.out.println("Computer:X in 3");System.out.println("Computer won.");break loop;}

}

}

if (turn!="4"){System.out.println("Computer:X in 4");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("6")){

System.out.println("Computer:X in 1");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("4")){

System.out.println("Computer:X in 5");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("3")){System.out.println("Computer:X in 9");System.out.println("Computer won.");break loop;}

if (turn.equals("9")){System.out.println("Computer:X in 3");System.out.println("Computer won.");break loop;}

if (turn!="3" & turn!="9"){System.out.println("Computer:X in 3");System.out.println("Computer won.");break loop;}

}

if (turn!="4"){System.out.println("Computer:X in 4");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("8")){

System.out.println("Computer:X in 1");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("4")){

System.out.println("Computer:X in 3");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("5")){System.out.println("Computer:X in 2");System.out.println("Computer won.");break loop;}

if (turn.equals("2")){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

if (turn!="5" & turn!="2"){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

}

if (turn!="4"){System.out.println("Computer:X in 4");System.out.println("Computer won.");break loop;} 

}

if (turn.equals("9")){

System.out.println("Computer:X in 1");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("4")){

System.out.println("Computer:X in 3");

System.out.println("Your turn:");

turn=scan.next();

if (turn.equals("5")){System.out.println("Computer:X in 2");System.out.println("Computer won.");break loop;}

if (turn.equals("2")){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

if (turn!="5" & turn!="2"){System.out.println("Computer:X in 5");System.out.println("Computer won.");break loop;}

}

if (turn!="4"){System.out.println("Computer:X in 4");System.out.println("Computer won.");break loop;} 

}

}System.out.println("\nNew Game");

}

 


}


}


  • Now execute the code.

No comments:

Post a Comment

Algorithmic Pixel Art: The Beauty of Aliasing

Discussed  here More images in  Google drive Some outputs: Forward to anyone who says aliasing is ugly.