Skip to content

Instantly share code, notes, and snippets.

@MisterDA
Last active April 16, 2018 16:53
Show Gist options
  • Save MisterDA/6e4f8029ef74a29f172a to your computer and use it in GitHub Desktop.
Save MisterDA/6e4f8029ef74a29f172a to your computer and use it in GitHub Desktop.
A Connect Four, CLI-based written in Java
import java.util.Scanner;
import java.util.Random;
public class Puissance4 {
public static void main (String[] args) {
if (args.length == 0) {
System.out.println("Player mode unspecified.\nUse PvP, PvC, CvP or CvC.");
return;
}
// true : is player
// false : is computer
boolean p1_type = true;
boolean p2_type = false;
// -1 : hazard
// 0 : winning shot depth 0
int p1_strat = 0;
int p2_strat = 0;
// true : player 1
// false : player 2
boolean turn = true;
int[][] board = new int[7][6];
for (int i = 0; i < 7; ++i) {
for (int j = 0; j < 6; ++j) {
board[i][j] = 0;
}
}
// Detect players, ask for strategy
if (args[0].equals("PvP")) {
p1_type = true;
p2_type = true;
} else if (args[0].equals("PvC")) {
p2_strat = askStrategy();
} else if (args[0].equals("CvP")) {
p1_type = false;
p2_type = true;
p1_strat = askStrategy();
} else if (args[0].equals("CvC")) {
p1_type = false;
p1_strat = askStrategy();
p2_strat = askStrategy();
} else {
System.out.println("Player mode unrecognized.\nUse PvP, PvC, CvP or CvC.");
return;
}
printBoard(board);
int weHaveAWinner = 0;
while (weHaveAWinner == 0) {
if (turn) { // player 1 turn
System.out.println("Yellow player's turn...");
if (p1_type) { // if he's a player
askColumn(board, 1);
} else { // he's a computer
ai(board, p1_strat, 1);
}
} else { // player 2 turn
System.out.println("Red player's turn...");
if (p2_type) { // if he's a player
askColumn(board, 2);
} else { // he's a computer
ai(board, p2_strat, 2);
}
}
weHaveAWinner = whoHasWon(board);
printBoard(board);
turn = !turn;
}
if ((p1_type == true && p2_type == false && weHaveAWinner == 2) ||
(p1_type == false && p2_type == true && weHaveAWinner == 1)) {
System.out.println("You have lost the Game !");
} else if (weHaveAWinner == 1) {
System.out.println("Yellow player has won !");
} else if (weHaveAWinner == 2) {
System.out.println("Red player has won !");
} else {
System.out.println("Tied !");
}
}
/**
* Prints the board.
*
* @param board The two-dimensional board.
*/
public static void printBoard (int[][] board) {
System.out.println("\n 1 2 3 4 5 6 7\n =============");
for (int j = 0; j < 6; ++j) {
System.out.print(" ");
for (int i = 0; i < 7; ++i) {
if (board[i][j] == 0) {
System.out.print((char)27 + "[90m." + (char)27 + "[0m ");
} else if (board[i][j] == 1) {
System.out.print((char)27 + "[33mY" + (char)27 + "[0m ");
} else if (board[i][j] == 2) {
System.out.print((char)27 + "[31mR" + (char)27 + "[0m ");
}
}
System.out.println();
}
System.out.println(" =============\n 1 2 3 4 5 6 7\n");
}
/**
* Ask for a computer strategy.
*
* @return MinMax tree depth.
*/
public static int askStrategy () {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Possible strategies:\n 0: hazard\n 1 -> n: difficulty");
System.out.print("Strategy: ");
int strategy = sc.nextInt();
if (strategy >= 0) {
return strategy - 1;
}
}
}
/**
* Ask for a Column.
*
* @param board The two-dimensional board.
* @param code The player's code. 1 or 2.
*/
public static void askColumn (int[][] board, int code) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("Column: ");
int col = sc.nextInt() - 1;
if (col >= 0 && col < 7) {
for (int j = 5; j >= 0; --j) {
if (board[col][j] == 0) {
board[col][j] = code;
return;
}
}
System.out.println("Column full !");
} else {
System.out.println("Invalid column !");
}
}
}
/**
* Loop on the board to fid a possible winner.
*
* @return Winner code.
* 0 : nobody has won yet
* 1 : player 1 has won
* 2 : player 2 has won
* 3 : tied !
*/
public static int whoHasWon (int[][] board) {
int p1 = 0;
int p2 = 0;
int tied = 0;
// loop on rows
for (int j = 0; j < 6; ++j) {
for (int i = 0; i < 7; ++i) {
if (board[i][j] == 0) {
p1 = 0;
p2 = 0;
} else if (board[i][j] == 1) {
++p1;
p2 = 0;
++tied;
} else if (board[i][j] == 2) {
p1 = 0;
++p2;
++tied;
}
if (p1 == 4) {
return 1;
} else if (p2 == 4) {
return 2;
}
}
p1 = 0;
p2 = 0;
}
if (tied == 42) {
return 3;
}
// loop on columns
for (int i = 0; i < 7; ++i) {
for (int j = 0; j < 6; ++j) {
if (board[i][j] == 0) {
p1 = 0;
p2 = 0;
} else if (board[i][j] == 1) {
++p1;
p2 = 0;
} else if (board[i][j] == 2) {
p1 = 0;
++p2;
}
if (p1 == 4) {
return 1;
} else if (p2 == 4) {
return 2;
}
}
p1 = 0;
p2 = 0;
}
// loop on diagonals left to right
for (int j = 0; j < 3; ++j) {
for (int i = 0; i < 4; ++i) {
if (board[i][j] == board[i+1][j+1]
&& board[i][j] == board[i+2][j+2]
&& board[i][j] == board[i+3][j+3]) {
if (board[i][j] == 1) {
return 1;
} else if (board[i][j] == 2) {
return 2;
}
}
}
}
// loop on diagonals right to left
for (int j = 0; j < 3; ++j) {
for (int i = 6; i >= 3; --i) {
if (board[i][j] == board[i-1][j+1]
&& board[i][j] == board[i-2][j+2]
&& board[i][j] == board[i-3][j+3]) {
if (board[i][j] == 1) {
return 1;
} else if (board[i][j] == 2) {
return 2;
}
}
}
}
return 0;
}
public static Random rand = new Random();
/**
* Controls computer's AI.
*
* @param board The two-dimensional board.
* @param depth The depth of the MiniMax algorithm tree. -1 for random.
* @param code The player's code. 1 or 2. (Maximazing player)
*/
public static int ai (int[][] board, int depth, int code) {
if (depth == -1) {
int col = 0;
while (true) {
col = rand.nextInt(7);
for (int j = 5; j >= 0; --j) {
if (board[col][j] == 0) {
board[col][j] = code;
System.out.println("Column: " + (col + 1));
return 0;
}
}
}
} else {
// TO DO later...
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment