Skip to content

Instantly share code, notes, and snippets.

@jdetle
Created March 3, 2015 04:40
Show Gist options
  • Save jdetle/2790797c2b72519aa719 to your computer and use it in GitHub Desktop.
Save jdetle/2790797c2b72519aa719 to your computer and use it in GitHub Desktop.
package tictactoe;
import java.util.Scanner;
public class HumanPlayer implements Player {
private Scanner scanner;
public char playerchar;
public HumanPlayer(Scanner s, char c){
scanner = s;
playerchar = c;
}
public boolean makemove(ToeBoard tictac){
int[] move = new int[2];
boolean win = false;
do{
move = tictac.receiveMove(scanner);
}while(tictac.checkmove(move)== false);
tictac.updateboard(move, playerchar);
win = tictac.checkwin(playerchar, move);
if(win == true){
tictac.printBoard();
System.out.println("\n Congratulations "+playerchar+", you won your game of Tic Tac Toe");
return true;
}
else{
tictac.printBoard();
return false;
}
}
package tictactoe;
public interface Player {
public boolean makemove(ToeBoard b);
}
package tictactoe;
import java.util.Scanner;
public class RunGame {
public static int choosenumberplayers(Scanner scanner){
System.out.print( "Greetings, human, would you like to play against me or another player? Enter 1 for me or 2 for another player \n");
String input = scanner.nextLine();
int inputint = Integer.parseInt(input);
if(inputint == 1 || inputint == 2){
return inputint;
}
else{
System.out.print("This number is not accepted, please try again \n");
inputint = choosenumberplayers(scanner);
return inputint;
}
}
public static int ReadPlayer1or2(Scanner scanner){
System.out.print( "Greetings, human, would you like to be player one or two? \n");
String input = scanner.nextLine();
int inputint = Integer.parseInt(input);
return inputint;
}
public static int ReadDimensions(Scanner scanner){
System.out.print( "Choose how many rows and columns you want to play with in your nxn game of tic-tac-toe\n");
String input = scanner.nextLine();
int inputint = Integer.parseInt(input);
return inputint;
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int numberplayers = 0;
int dimension = 0;
boolean gamewon = false;
dimension = ReadDimensions(scanner);
numberplayers = choosenumberplayers(scanner);
ToeBoard TB = new ToeBoard(dimension);
TB.printBoard();
System.out.println("");
Player player1 = new HumanPlayer(scanner, 'X');
Player player2 = new HumanPlayer(scanner, 'O');
while(!gamewon){
gamewon = player1.makemove(TB);
if(gamewon == true){
break;
}
gamewon = player2.makemove(TB);
}
}
}
package tictactoe;
import java.awt.List;
import java.util.ArrayList;
import java.util.Scanner;
public class ToeBoard {
int n; //nxn dimension board
char[][] XOarray = new char[n][n];
int numbermoves = 0;
//initializes TicTacToe Board's dashes in between rows
private String dashes(int n){
int numberdashes = n*2;
String dashline = "";
for(int i=0; i<numberdashes; i++){
dashline = dashline+"-";
}
return dashline;
}
//dash is now computed only once for each instance of the class
String dash = dashes(n);
public ToeBoard(int n){
this.n = n;
this.XOarray = new char[n][n];
this.dash = dashes(n);
for (int i=0; i<n; i++){
for(int j=0; j<n; j++){
XOarray[i][j] = ' ';
}
}
}
public ToeBoard(char[][] XOarray) {
this.n = XOarray.length;
this.dash = dashes(n);
this.XOarray = new char[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
this.XOarray[i][j] = XOarray[i][j];
}
public ToeBoard(ToeBoard A) { this(A.XOarray); }
public void printBoard(){
ToeBoard tictac = this;
System.out.print("\n");
for(int i=0; i<tictac.n; i++){
System.out.print(tictac.XOarray[i][0]);
for(int j=1; j<tictac.n; j++){
System.out.print("|"+tictac.XOarray[i][j]);
}
if(i<tictac.n-1){
System.out.print("\n"+tictac.dash+"\n");
}
}
}
public boolean checkmove(int[] playermove){
ToeBoard tictac = this;
int iposition = playermove[0];
int jposition = playermove[1];
if(iposition >= tictac.n || jposition >= tictac.n){
System.out.println("You chose a position that does not exist, please try again");
return false;
}
if(tictac.XOarray[iposition][jposition] == ' '){
return true;
}
else{
System.out.println("This position is already taken, please choose another.");
return false;
}
}
public int[] receiveMove(Scanner scanner){
int[] move = new int[2];
System.out.print("\nPlease choose the row that you want to place your move in.\n");
String input = scanner.nextLine();
int inputint = Integer.parseInt(input);
//array index starts at 0 so subtract one
move[0] = inputint-1;
System.out.print("\nPlease choose the column that you want to place your move in.\n");
input = scanner.nextLine();
inputint = Integer.parseInt(input);
//array index starts at 0 so subtract one
move[1] = inputint-1;
return move;
}
public void updateboard(int[] playermove, char playerchar){
ToeBoard tictac = this;
tictac.XOarray[playermove[0]][playermove[1]] = playerchar;
}
public boolean diagwin(char playerchar){
ToeBoard tictac = this;
//System.out.print(tictac.n);
for(int i=0; i<tictac.n; i++){
//guaranteed if else
if(tictac.XOarray[i][i] != playerchar){
return false;
}
}
return true;
}
public boolean antidiagwin(char playerchar){
ToeBoard tictac =this;
for(int i=0; i<tictac.n; i++){
if(tictac.XOarray[i][tictac.n-i-1] != playerchar){
return false;
}
}
return true;
}
public boolean verticalwin(char playerchar, int index){
ToeBoard tictac = this;
for(int i=0; i<tictac.n; i++){
//guaranteed if else
if(tictac.XOarray[i][index] != playerchar){
return false;
}
}
return true;
}
public boolean horizontalwin(char playerchar, int index){
ToeBoard tictac = this;
for(int i=0; i<tictac.n; i++){
//guaranteed if else
if(tictac.XOarray[index][i] != playerchar){
return false;
}
}
return true;
}
public boolean checkwin(char playerchar, int[] move){
ToeBoard tictac = this;
boolean result = false;
result = tictac.diagwin(playerchar);
if(result == true){
return result;
}
else{
//System.out.println("usingverticalwin");
result = tictac.antidiagwin(playerchar);
if(result == true){
return result;
}
else{
result = tictac.verticalwin(playerchar, move[1]);
if(result == true){
return result;
}
else{
//System.out.println("usinghorizontalwin");
result = tictac.horizontalwin(playerchar, move[0]);
if(result == true){
return result;
}
else{
return false;
}
}
}
}
}
//assesses board for all possible moves using brute force
public 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;
playable.add(move);
}
}
}
return playable;
}
//Function plays all possible games
public class WinDrawLose {
int[] move = new int[2];
boolean win = false;
boolean lose = false;
boolean draw = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment