Skip to content

Instantly share code, notes, and snippets.

@sohailbud
Created April 20, 2015 19:19
Show Gist options
  • Save sohailbud/c3180e11d802cbbb4db2 to your computer and use it in GitHub Desktop.
Save sohailbud/c3180e11d802cbbb4db2 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
public class Computer {
/**
* Computer finds out if the player can win in next turn and prevents player from winning
* If player cannot win in next turn, computer finds the best possible option and plays it
* @param outcomes
* @param TicTacToe
* @return
*/
public String[] play(List<Integer> outcomes, String[] TicTacToe) {
Integer playPosition = null;
if (playerAlmostWon(outcomes, TicTacToe)==null) {
List<Integer> possibleChoices = new ArrayList<Integer>();
for (int i=0; i<outcomes.size(); i++) {
String str = String.valueOf(outcomes.get(i));
char[] digits = str.toCharArray();
List<Integer> temp = new ArrayList<Integer>();
for(int j=0; j<digits.length; j++) {
if(TicTacToe[Character.digit(digits[j],10)-1]==" ") {
temp.add(Character.digit(digits[j],10));
} else {
temp.clear();
}
}
possibleChoices.addAll(temp);
}
playPosition=mode(possibleChoices);
} else {
playPosition=playerAlmostWon(outcomes, TicTacToe);
}
if (TicTacToe[playPosition-1]==" ") {
TicTacToe[playPosition-1]="O";
}
System.out.println("Computer plays...");
System.out.println(TicTacToe[0]+" | "+TicTacToe[1]+" | "+TicTacToe[2]+"\n"+"‾‾‾‾‾‾‾‾‾"+"\n"+
TicTacToe[3]+" | "+TicTacToe[4]+" | "+TicTacToe[5]+"\n"+"‾‾‾‾‾‾‾‾‾"+"\n"+
TicTacToe[6]+" | "+TicTacToe[7]+" | "+TicTacToe[8]);
return TicTacToe;
}
/**
* Simple method to check if computer won
* @param outcomes
* @param TicTacToe
* @return
*/
public boolean didWin(List<Integer> outcomes, String[] TicTacToe) {
boolean won = false;
for (int i=0; i<outcomes.size(); i++) {
String str = String.valueOf(outcomes.get(i));
char[] digits = str.toCharArray();
int filled = 0;
for(int j=0; j<digits.length; j++) {
if(TicTacToe[Character.digit(digits[j],10)-1]=="O") {
filled++;
}
}
if(filled==3) {
won=true;
System.out.println("COMPUTER WON, You really suck at this!");
}
}
return won;
}
/**
* Computer uses this method to check if player can win in next turn
* @param outcomes
* @param TicTacToe
* @return
*/
private Integer playerAlmostWon(List<Integer> outcomes, String[] TicTacToe) {
Integer missing = null;
for (int ii=0; ii<outcomes.size(); ii++) {
String str = String.valueOf(outcomes.get(ii));
char[] digits = str.toCharArray();
int filled = 0;
missing=null;
for(int j=0; j<digits.length; j++) {
if(TicTacToe[Character.digit(digits[j],10)-1]=="X") {
filled++;
} else if(TicTacToe[Character.digit(digits[j],10)-1]==" ") {
missing = Character.digit(digits[j],10);
}
}
if(filled==2) {
break;
} else {
missing = null;
}
}
return missing;
}
/**
* Finds mode of list of int array
* @param a
* @return
*/
private int mode(List<Integer> a) {
int maxValue = 0;
int maxCount = 0;
for (int i = 0; i < a.size(); ++i) {
int count = 0;
for (int j = 0; j < a.size(); ++j) {
if (a.get(j) == a.get(i)) ++count;
}
if (count > maxCount) {
maxCount = count;
maxValue = a.get(i);
}
}
return maxValue;
}
}
import java.util.List;
import java.util.Scanner;
public class Player {
/**
* Takes a user input from player and plays at that position
* If player accidentally puts a position that has been already taken, it gives the player option to play again
* @param TicTacToe
* @return
*/
public String[] play(String[] TicTacToe) {
Scanner in = new Scanner(System.in);
System.out.println("Your turn!"+"\n"+"Chose a position between 1 and 9");
String userInput = in.nextLine();
if (TicTacToe[Integer.parseInt(userInput)-1]==" ") {
TicTacToe[Integer.parseInt(userInput)-1]="X";
} else if(TicTacToe[Integer.parseInt(userInput)-1]=="X" || TicTacToe[Integer.parseInt(userInput)-1]=="O") {
System.out.println("Position taken, please chose another spot");
play(TicTacToe);
}
System.out.println(TicTacToe[0]+" | "+TicTacToe[1]+" | "+TicTacToe[2]+"\n"+"‾‾‾‾‾‾‾‾‾"+"\n"+
TicTacToe[3]+" | "+TicTacToe[4]+" | "+TicTacToe[5]+"\n"+"‾‾‾‾‾‾‾‾‾"+"\n"+
TicTacToe[6]+" | "+TicTacToe[7]+" | "+TicTacToe[8]);
return TicTacToe;
}
/**
* Simple method to check if player won
* @param outcomes
* @param TicTacToe
* @return
*/
public boolean didWin(List<Integer> outcomes, String[] TicTacToe) {
boolean won = false;
for (int i=0; i<outcomes.size(); i++) {
String str = String.valueOf(outcomes.get(i));
char[] digits = str.toCharArray();
int filled = 0;
for(int j=0; j<digits.length; j++) {
if(TicTacToe[Character.digit(digits[j],10)-1]=="X") {
filled++;
}
}
if(filled==3) {
won=true;
System.out.println("YOU WON!!!");
}
}
return won;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Runner {
/**
* Creates Tic-Tac-Toe board, creates objects for player and computer and calls play method on them
* Checks if either computer or player won by calling won method
* Checks to see if its a tie
* @param args
*/
public static void main(String[] args){
// All possible outcomes to win
List<Integer> outcomes = new ArrayList<Integer>();
outcomes.add(123);
outcomes.add(456);
outcomes.add(789);
outcomes.add(147);
outcomes.add(258);
outcomes.add(369);
outcomes.add(159);
outcomes.add(357);
// Tic-Tac-Toe Board
String[] TicTacToe = {" "," "," "," "," "," "," "," "," "};
// Create Player and Computer objects
Player player = new Player();
Computer computer = new Computer();
// PLAY
for(int turnsLeft=5; turnsLeft>0; turnsLeft--) {
if(Arrays.asList(TicTacToe).contains(" ")) {
TicTacToe=player.play(TicTacToe);
} else {
System.out.println("ITS A TIE :(");
break;
}
if (player.didWin(outcomes, TicTacToe)) {
break;
}
if(Arrays.asList(TicTacToe).contains(" ")) {
TicTacToe=computer.play(outcomes, TicTacToe);
} else {
System.out.println("ITS A TIE :(");
break;
}
if (computer.didWin(outcomes, TicTacToe)) {
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment