Created
March 4, 2015 04:30
-
-
Save jdetle/7d7c814be9293faf2ee8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tictactoe; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
public class ComputerPlayer implements Player { | |
public char playerchar; | |
public ComputerPlayer(char c){ | |
playerchar = c; | |
} | |
private ArrayList<int[]> playablemoves(ToeBoard tictac){ | |
ArrayList<int[]> playable = new ArrayList<int[]>(); | |
int[] move = new int[2]; | |
for(int i=0; i<tictac.n; i++){ | |
for(int j=0; j<tictac.n; j++){ | |
if(tictac.XOarray[i][j] == '-'){ | |
move[0] = i; | |
move[1] = j; | |
//System.out.print("\nPlayable i,j ="+move[0]+" "+move[1]); | |
playable.add(move); | |
} | |
} | |
} | |
return playable; | |
} | |
private void checkplayable(ToeBoard tictac){ | |
int[] move = new int[2]; | |
ArrayList<int[]> playable = new ArrayList<int[]>(); | |
playable = playablemoves(tictac); | |
System.out.println(playable.size()); | |
for(int i=0; i<playable.size(); i++){ | |
move = playable.get(i); | |
System.out.println("\n"+move[0]+" "+move[1]); | |
} | |
} | |
private WinDrawLose returnmoveoutcome(ToeBoard tictac, char playerchar){ | |
//System.out.println("Remember to input your move in terms of the row and column that you want to place your character in."); | |
char nextplayerchar = 'O'; | |
if(playerchar == 'O'){ | |
nextplayerchar = 'X'; | |
} | |
WinDrawLose bestOutcome = null; | |
int[] move = new int[2]; | |
ArrayList<int[]> playable = new ArrayList<int[]>(); | |
playable = playablemoves(tictac); | |
int movesleft = playable.size(); | |
ToeBoard[] boardsaftermove = new ToeBoard[movesleft]; | |
int[][] moves = new int[playable.size()][2]; | |
//System.out.println("\n"+playable.size()); | |
//for(int i=0; i<movesleft; i++){ | |
//move = playable.get(i); | |
//System.out.println(move[0]+" "+move[1]); | |
//} | |
for(int i=0; i<movesleft; i++){ | |
//if(move[0] == 1 && move[1] == 0){ | |
//System.out.println("\n 1,0 checked! "+playerchar); | |
//} | |
//System.out.println(i); | |
//boardsaftermove[i].printBoard(); | |
boardsaftermove[i] = new ToeBoard(tictac); | |
move = playable.get(i); | |
moves[i][0] = move[0]; | |
moves[i][1] = move[1]; | |
boardsaftermove[i].updateboard(move, playerchar); | |
if(boardsaftermove[i].checkwin(playerchar, move) == true){ | |
WinDrawLose outcome = new WinDrawLose(); | |
outcome.move= move; | |
outcome.win = true; | |
return outcome; | |
} | |
} | |
if(movesleft == 1){ | |
WinDrawLose outcome = new WinDrawLose(); | |
outcome.move= move; | |
outcome.draw = true; | |
return outcome; | |
} | |
for(int i=0; i<movesleft; i++){ | |
WinDrawLose nextoutcome = returnmoveoutcome(boardsaftermove[i], nextplayerchar); | |
nextoutcome.reverse(); | |
if (bestOutcome == null || nextoutcome.betterThan(bestOutcome)) { | |
bestOutcome = nextoutcome; | |
} | |
} | |
// End for loop | |
return bestOutcome; | |
} | |
public boolean makemove(ToeBoard tictac) { | |
int[] move = new int[2]; | |
char otherplayer = 'X'; | |
if(playerchar == 'X'){ | |
otherplayer = 'O'; | |
} | |
WinDrawLose moveoutcome = new WinDrawLose(); | |
boolean win = false; | |
do{ | |
moveoutcome = returnmoveoutcome(tictac, playerchar); | |
move = moveoutcome.move; | |
System.out.println(move[0]+""+move[1]); | |
}while(tictac.checkmove(move)==false); | |
tictac.updateboard(move, playerchar); | |
win = tictac.checkwin(playerchar, move); | |
if(win == true){ | |
tictac.printBoard(); | |
System.out.println("\n Sorry,"+otherplayer+", you lost your game of Tic Tac Toe"); | |
return true; | |
} | |
else{ | |
System.out.print("\nThis is my move, human...\n"); | |
tictac.printBoard(); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment