You are on page 1of 4

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Home
*/
import java.util.ArrayList;
import java.util.Scanner;
public class Grid {
int arb_large=20000;
int[][] a;
private int player;//Stores Current player//computer = 1;//user = -1
int user=-1,comp=1;
int oMoves=0;
public void changePlayer(){
player=player==user?comp:user;
}
public int playerPoints(){
return player==user?-20-oMoves:20+oMoves;
}

public Grid() {
this.a = new int[3][3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
this.a[i][j]=0;
player=user;
}

public Grid(Grid dup){


this.a = new int[3][3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
this.a[i][j]=dup.a[i][j];
this.oMoves=dup.oMoves;
player=dup.player;
}

boolean isEmpty(int loc){


if(a[loc/3][loc%3]==0)
return true;
return false;

int[] emptyCells()/*finds and returns empty cells*/{


ArrayList<Integer> arr;
arr = new ArrayList<>(9);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(this.a[i][j]!=1&&this.a[i][j]!=-1)
arr.add(i*3+j);
int[] ret = new int[arr.size()];
for(int i = 0;i < ret.length;i++)
ret[i] = arr.get(i);
return ret;
}

boolean check(){
for(int i=0;i<3;i++){
if(a[i][0]==player && a[i][1]==player && a[i][2]==player)
return true;
if(a[0][i]==player && a[1][i]==player && a[2][i]==player)
return true;
}
if(a[0][0]==player && a[1][1]==player && a[2][2]==player)
return true;
if(a[2][0]==player && a[1][1]==player && a[0][2]==player)
return true;
return false;
}

void display(){
System.out.println("\n"+convert(a[0][0])+"|"+convert(a[0][1])
+"|"+convert(a[0][2]));
System.out.println("-|-|-");
System.out.println(convert(a[1][0])+"|"+convert(a[1][1])+"|"+convert(a[1]
[2]));
System.out.println("-|-|-");
System.out.println(convert(a[2][0])+"|"+convert(a[2][1])+"|"+convert(a[2]
[2]));
}

char convert(int value){


if(value==user)
return 'O';
if(value==comp)
return 'X';
return ' ';
}

public void start(int number){


display();
for(int i=0;i<9;i++){
int pos;
if(number==2||player==user)
pos=user();
else
pos=move();
oMoves+=1;
a[pos/3][pos%3]=player;
display();
if(check()){
System.out.println("Player:"+convert(player)+" Won!");
System.exit(0);
}

changePlayer();
}
System.out.println("Draw...");

}
int user(){
boolean flag=false;
Scanner sc=new Scanner(System.in);
int pos;
do{
System.out.println("Enter position(1-9): ");
pos=sc.nextInt();
if(pos>9||pos<1||!isEmpty(pos-1))
System.out.println("Invalid position");
else{
//a[pos/3][pos%3]=player;
flag=true;
}
}while(!flag);
//sc.close();
return pos-1;
}
int move(){

int moves[]=play(new Grid(this));


int best=(int)(Math.random()*9);
for(int i=0;i<9;i++){
if(!isEmpty(best))
best=i;
if(isEmpty(i) && moves[i]>moves[best])
best=i;
}
return best;
}

void mark_grid(int move){


this.a[move/3][move%3]=this.player;
}
int max(int[] arr){
int mx=-arb_large;
for(int x:arr){
if(x>200)
continue;
mx=x>mx?x:mx;
}
return mx;
}
int min(int[] arr){
int mn=arb_large;
for(int x:arr)
mn=x<mn?x:mn;
return mn;
}
int[] play(Grid temp){
int possible[]=temp.emptyCells();
int points[]=new int[9];
for(int i=0;i<9;i++){
points[i]=arb_large;
}
for(int x:possible)
points[x]=0;
if(possible.length==1){
temp.mark_grid(possible[0]);
temp.oMoves+=1;
if(temp.check())
points[possible[0]]=temp.playerPoints();
else
points[possible[0]]=0;
return points;
}
for(int x:possible){
Grid t=new Grid(temp);
t.mark_grid(x);
t.oMoves+=1;
if(t.check())
points[x]=t.playerPoints();
else{
t.changePlayer();
int[] temp_points=play(t);
if(temp.player==comp)
points[x]=min(temp_points);
else
points[x]=max(temp_points);
}
}
return points;

public class Test {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of players: ");
int number=sc.nextInt();
if(number!=2 && number!=1){
System.out.printf("Wrong number of players");
System.exit(0);
}
Grid demoGame=new Grid();
demoGame.changePlayer();
demoGame.start(number);
}
}

You might also like