Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tictactoefull;
- import java.util.InputMismatchException;
- import java.util.Random;
- import java.util.Scanner;
- public class TicTacToeFULL {
- public static void main(String[] args) {
- int index=0,win=0,choose=0;
- boolean done = false;
- do{
- try{
- System.out.println("For Single Player press 1");
- System.out.println("For Double Player press 2");
- System.out.print("Choice: ");
- Scanner input = new Scanner(System.in);
- choose = input.nextInt();
- if(choose==1 || choose==2){done = true;}
- else{
- printProblem();
- }
- }catch(InputMismatchException e){
- printProblem();
- }
- }while(!done);
- done = false;
- Board board = new Board();
- Sign player1 = new Sign();
- if(choose == 2){
- Sign player2 = new Sign();
- do{
- System.out.print("Input 1st Player Name or Sign: ");
- player1.setSign();
- System.out.print("Input 2nd Player Name or Sign: ");
- player2.setSign();
- if(player1.getSign() == player2.getSign() || !player1.isDone() || !player2.isDone()){
- System.out.println("Sign Clash or Invalid Sign !!!");
- }
- }while(player1.getSign() == player2.getSign() || !player1.isDone() || !player2.isDone());
- board.printBoard();
- for(int i=0; i<9; i++){
- if(board.cheakWin()){
- if(cheakPlayer1(i)){
- win = 1;
- System.out.printf("%s Win the Game !!\n",player2.getPlayerName());
- }
- else{
- win = 1;
- System.out.printf("%s Win the Game !!\n",player1.getPlayerName());
- }
- break;
- }
- do{
- if(cheakPlayer1(i)){
- System.out.printf("%s set %c in the board...\n",
- player1.getPlayerName(),player1.getSign());
- }
- else{
- System.out.printf("%s set %c in the board...\n",
- player2.getPlayerName(),player2.getSign());
- }
- do{
- try{
- System.out.print("Select [1 to 9]: ");
- Scanner input = new Scanner(System.in);
- index = input.nextInt();
- done = true;
- }catch(InputMismatchException e){
- printProblem();
- }
- }while(!done);
- done = false;
- }while(!board.testValidity(index));
- if(cheakPlayer1(i)){
- board.setIndex(index, player1.getSign());
- }
- else{
- board.setIndex(index, player2.getSign());
- }
- board.printBoard();
- }
- if(win==0 && !board.cheakWin() ){
- System.out.println();
- System.out.println("Game drow.... !!!");
- }
- else if (win != 1){
- System.out.printf("%s Win the Game !!\n",player1.getPlayerName());
- }
- }
- //play with computer
- else{
- char computerSign;
- int gameMode=0,attemp=0;
- System.out.println("Two Modes are available Easy & Hard");
- do{
- try{
- System.out.println("For Easy Mode press 1");
- System.out.println("For Hard Mode press 2");
- System.out.print("Game Mode: ");
- Scanner input = new Scanner(System.in);
- gameMode = input.nextInt();
- if(gameMode==1 || gameMode==2){done = true;}
- else{
- printProblem();
- }
- }catch(InputMismatchException e){
- printProblem();
- }
- }while(!done);
- done = false;
- do{
- System.out.print("Input Your Name or Sign: ");
- player1.setSign();
- if(!player1.isDone()){
- System.out.println("Invalid Sign !!!");
- }
- }while(!player1.isDone());
- //setting computer's sign
- if(player1.getSign() == 'X'){
- computerSign = 'O';
- }
- else{
- computerSign = 'X';
- }
- System.out.println("Computer will play with sign: "+ computerSign);
- board.printBoard();
- for(int i=0; i<9; i++){
- if(board.cheakWin()){
- if(cheakPlayer1(i)){
- win = 1;
- System.out.println("Computer win... !!!");
- }
- else{
- win = 1;
- System.out.println("You win the Game... !!!");
- }
- break;
- }
- if(cheakPlayer1(i)){
- System.out.println("Place " + player1.getSign() + " in the Board");
- do{
- do{
- try{
- System.out.print("Select [1 to 9]: ");
- Scanner input = new Scanner(System.in);
- index = input.nextInt();
- done = true;
- }catch(InputMismatchException e){
- printProblem();
- }
- }while(!done);
- done = false;
- if(!board.testValidity(index)){
- printProblem();
- }
- }while(!board.testValidity(index));
- board.setIndex(index, player1.getSign());
- }
- else{
- //computers turn calculation
- System.out.println("Computer's Turn...");
- Random generator = new Random();
- do{
- if(gameMode == 2 && attemp==0){
- index = board.setTarget(player1.getSign(),computerSign);
- attemp++;
- }
- else{
- index = generator.nextInt(10);
- }
- }while(!board.testValidity(index));
- attemp =0;
- System.out.println("Computer choose Index: "+index);
- board.setIndex(index, computerSign);
- }
- board.printBoard();
- }
- if(win==0 && !board.cheakWin() ){
- System.out.println();
- System.out.println("Game drow.... !!!");
- }
- else if (win != 1){
- System.out.printf("Congratulation You Win the Game... !!");
- }
- }
- }
- public static boolean cheakPlayer1 (int n){
- if(n==0 || n==2 || n==4 || n==6 || n==8){
- return true;
- }
- return false;
- }
- public static void printProblem(){
- System.out.println("Wrong Input...!!!");
- System.out.println("Input Again...!!!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment