Tic Tac Toe is classic game played using pen and paper.
Wikipedia article on tic tac toe:-
https://en.wikipedia.org/wiki/Tic-tac-toe
Below is the code for implementation of this all time classic in Java language.
Tic-Tac-Toe |
Java Code
import java.util.Scanner; public class xotester { public static void main(String[] args) { int choice=1; groundclass a ; Scanner select=new Scanner(System.in); statfunction.intro(); statfunction.xointro(); while(choice==1){ a = new groundclass(); a.toss(); a=null;//calling garbage collector; System.out.println("press 1 to play another game else zero "); choice=select.nextInt(); } System.out.println("the program is now terminating"); System.out.println("we are beleivers of open source software"); System.out.println("show ur support by visiting www.facebook.com/prgwonders"); select.close(); } } //this is to prevent creation of instance of this class abstract class statfunction{ public static void intro(){ //this has regular stuff regarding bullseye System.out.println("TIC TAC TOE#v1.0" ); System.out.println("Program by Abhishek Munagekar"); System.out.println("Development starts:-18/3/15"); System.out.println("Last edited on:-20/03/2015"); System.out.println("For more Visit www.facebook.com/prgwonders"); System.out.println("This is a Two Player game only"); } public static void xointro(){ int i,j; System.out.println("The ground position numbers have been illustrated below"); for(i=1;i<=3;i++){ for(j=1;j<=3;j++){ System.out.print((i*10+j)+"\t"); } System.out.print("\n"); } System.out.println("Ur objective is to make three x's or o's.\nKindly refer for rules from somewhere else:-)"); System.out.println("Symbol for player 1 is X and that for player 2 is O"); } public static void end(int victorystate){ switch (victorystate){ case 1: System.out.println("Congratulations Player X wins"); break; case 2: System.out.println("congratulations Player O wins"); break; case 0: System.out.println("Its a tie"); break; } } } class groundclass{ int victorystate=0,playertoplay,movecount=0; int[][] ground=new int[3][3]; //constructor groundclass(){ int i,j; for(i=0;i<=2;i++) for(j=0;j<=2;j++) ground[i][j]=(-100); } //constructor ends here //below is a debugging tool to print ground state void groundprint(){ int i,j; for(i=0;i<=2;i++) for(j=0;j<=2;j++) System.out.print(ground[i][j]+"\t"); } //below is a ground feeding tool void groundfeeding(){ Scanner groundfeeder = new Scanner( System.in ); int i,j; for(i=0;i<=2;i++) for(j=0;j<=2;j++){ System.out.println("Enter value for " +(i+1)+(j+1) ); ground[i][j]=groundfeeder.nextInt(); } groundfeeder.close(); }//groundfeeder ends //below is a ground impossible test this is based only on the x and o count boolean groundrig(){ boolean rigged=false; int i,j,p1=0,p2=0,breakcounter=0; for(i=0;i<=2;i++) { if(breakcounter==1) break; for(j=0;j<=2;j++){ if(ground[i][j]==(-100)){ continue; } else if(ground[i][j]==1){ p1=p1+1; } else if(ground[i][j]==20){ p2=p2+1; } //checks for any integer other than expected else{ rigged=true; System.out.println("inappropriate integer\t error at"+(i+1)+(j+1)); breakcounter=1; break; } } } if((rigged==false)&&(p1-p2>=2||p2-p1>=2)){ rigged=true; System.out.println("move count mismatch"); } if(rigged==true) System.out.println("this ground is rigged"); if(rigged==false) System.out.println("this is ground is not rigged"); return rigged; } void toss(){ playertoplay=(int)(Math.random()*2); //remove below comment tag for debugging //System.out.println(playerno); /*System.out.println("Player "+(playertoplay+1)+" plays first"); if(playertoplay==0) System.out.println("X plays first"); else System.out.println("O plays first");*/ //pls note that playertoplay varies from 0 to 1 this.victorychecker(); } void victorychecker(){ int i,j,a,sum; if(movecount>=5){ //horizontal chker below for(i=0;i<=2;i++){ sum=0; for(j=0;j<=2;j++) sum=sum+ground[i][j]; if(sum==3){ victorystate=1; break; } else if(sum==60){ victorystate=2; break; } } //horizontal victory checker ends //vertical victory checker starts if(victorystate==0){ for(j=0;j<=2;j++){ sum=0; for(i=0;i<=2;i++) sum=sum+ground[i][j]; if(sum==3){ victorystate=1; break; } else if(sum==60){ victorystate=2; break; } } } //vertical victory checking ends //first diagonal checking starts if(victorystate==0){ sum=0; for(a=0;a<=2;a++) sum=sum+ground[a][a]; if(sum==3){ victorystate=1; } else if(sum==60){ victorystate=2; } } //first diagonal checking ends //second diagonal checking starts if(victorystate==0){ sum=0; for(i=0,j=2;i<=2;i++,j--) sum=sum+ground[i][j]; if(sum==3){ victorystate=1; } else if(sum==60){ victorystate=2; } } //second diagonal checking ends }//movecount wala if ends this.display(); }//victory checker ends here void display(){ int i,j; System.out.println("GRID"); for(i=0;i<=2;i++){ System.out.println("\n"); for(j=0;j<=2;j++){ if(ground[i][j]==-100){ System.out.print(((i+1)*10+(j+1))+"\t"); } else if(ground[i][j]==1){ System.out.print("X\t"); } else { System.out.print("O\t"); } } } switch(playertoplay){ case 0: if(victorystate==0) System.out.print("\n X to play"); break; case 1: if(victorystate==0) System.out.print("\nO to play"); } System.out.print("\tMOVECOUNT:-"+movecount+"\n"); this.movemaker(); }//end of display function public void movemaker(){ if(victorystate==1||victorystate==2) statfunction.end(victorystate); else if(victorystate==0&&movecount==9) statfunction.end(victorystate); else{Scanner input=new Scanner(System.in); int i,j,cords,flag=0; do{ System.out.println("Enter the coord where u want to make the move"); cords=input.nextInt(); i=(cords/10)-1; j=(cords%10)-1; flag=0; if(i>2||i<0||j>2||j<0){ flag=1; System.out.println("please reenter cordinates"); } if(flag==0&&ground[i][j]!=-100){ flag=1; System.out.println("Illegal move"); } if(flag==0&&playertoplay==0){ ground[i][j]=1; } if(flag==0&&playertoplay==1){ ground[i][j]=20; } } while(flag==1); playertoplay++; movecount++; playertoplay=playertoplay%2; this.victorychecker(); } }//end of movemaker }
This code is also converted into an exe file for 32 bit windows OS using Jsmooth utility. You can find the above source code( in txt and pdf format) along with the exe at http://www.mediafire.com/download/lds5aihzuo3xoi5/tic+tac+toe.zip
No comments:
Post a Comment