import java.io.*;
import java.util.*;
public class PegGame {
public static String GetInput() {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String i = "";
try {
i = reader.readLine();
}
catch (Exception e) {
System.out.println("Error with input");
}
return i;
}
public static void initialboard(String board[][]) {
int c = 0;
int e = 0;
//prints out 'O' in any spot that isn't taken
while(c<board.length){
while(e<board[c].length){
board[c][e]="O";
e=e+1;
}
c=c+1;
e=0;
}
//numbers
board[0][1] = "1";
board[0][2] = "2";
board[0][3] = "3";
board[0][4] = "4";
board[0][5] = "5";
board[0][6] = "6";
board[0][7] = "7";
board[1][0] = "1";
board[2][0] = "2";
board[3][0] = "3";
board[4][0] = "4";
board[5][0] = "5";
board[6][0] = "6";
board[7][0] = "7";
//empty spots
board[0][0] = " ";
board[1][1] = "-";
board[1][2] = "-";
board[2][1] = "-";
board[2][2] = "-";
board[1][6] = "-";
board[1][7] = "-";
board[2][6] = "-";
board[2][7] = "-";
board[6][1] = "-";
board[6][2] = "-";
board[7][1] = "-";
board[7][2] = "-";
board[6][6] = "-";
board[6][7] = "-";
board[7][6] = "-";
board[7][7] = "-";
board[4][4] = " ";
}
public static void displayboard(String board[][]) {
//prints out all the spots in the array
int c = 0;
int e = 0;
while(c<board.length) {
while(e<board[c].length) {
System.out.print(board[c][e]+" ");
e=e+1;
}
e=0;
c=c+1;
System.out.print("\n");
}
}
public static boolean checkmoves(String board[][]) {
//row = r, column = c
int r = 1;
int c = 1;
//if there are no moves, it returns true
//checks right and down
while(r<board.length-2){
while(c<board.length-2) {
if(board[r][c].equals("O")&&board[r+1][c].equals("O")&&board[r+2][c].equals(" ")) {
return true;
}
if(board[r][c].equals("O")&&board[r][c+1].equals("O")&&board[r][c+2].equals(" ")) {
return true;
}
c=c+1;
}
c=0;
r=r+1;
}
r=board.length-1;
c=board.length-1;
//checks left and up
while(r>2){
while(c>2) {
if(board[r][c].equals("O")&&board[r-1][c].equals("O")&&board[r-2][c].equals(" ")) {
return true;
}
if(board[r][c].equals("O")&&board[r][c-1].equals("O")&&board[r][c-2].equals(" ")) {
return true;
}
c=c-1;
}
c=board.length-1;
r=r-1;
}
//return false if there is at least one move left
return false;
}
public static int checkpegs(String board[][], int pegs) {
//row = r, column = c
int r = 1;
int c = 1;
//checks the entire array for any 'O'
while(r<board.length){
while(c<board.length) {
if(board[r][c].equals("O")) {
pegs = pegs + 1;
}
c=c+1;
}
c=0;
r=r+1;
}
return pegs;
//looks for all the remaining pegs in the board and returns it
}
public static void main(String args[]) {
//declare variables
String board[][] = new String[8][8];
String choice = "Yes";
boolean winner = false;
int sr = 0;
int sc = 0;
int er = 0;
int ec = 0;
int score = 10;
int pegs = 0;
//play again loop
while (choice.compareToIgnoreCase("No")!=0&&choice.compareToIgnoreCase("N")!=0) {
//re-declare variables
winner = false;
pegs = 0;
score = 10;
System.out.println("Peg Game. 'O' are pegs and '-' are unusable");
System.out.println("Every peg leftover at the end of the game is -1 point from 10.");
System.out.println();
initialboard(board);
//game loop
while(!winner) {
//re-displays board everytime there is input
displayboard(board);
//calls checkmoves function to see if there's any moves left
if(!checkmoves(board)) {
pegs = checkpegs(board, pegs);
score = score - pegs;
System.out.println("No more moves, your score is " + score);
//call checkpegs function when no more moves to count the remaining pegs
break;
}
// get input and change it into int's so you can put it into the board array
// ask for a start and end point
System.out.print("Enter the start row ");
sr = Integer.parseInt(GetInput());
System.out.print("Enter the start column ");
sc = Integer.parseInt(GetInput());
System.out.print("Enter the end row ");
er = Integer.parseInt(GetInput());
System.out.print("Enter the end column ");
ec = Integer.parseInt(GetInput());
// left right movements
if(sr==er&&!board[sr][sc].equals("-")&&!board[er][ec].equals("-")) {
if(sc>ec) {
if(board[er][ec].equals(" ")&&board[sr][sc].equals("O")&&board[sr][sc-1].equals("O")) {
//left
board[er][ec] = "O";
board[sr][sc] = " ";
board[sr][sc-1] = " ";
}
else {
System.out.println("Invalid move");
}
}
else{
//right
board[er][ec] = "O";
board[sr][sc] = " ";
board[sr][sc+1] = " ";
}
}
// up down movements
else if(sc==ec&&!board[sr][sc].equals("-")&&!board[er][ec].equals("-")) {
if(sr>er) {
if(board[er][ec].equals(" ")&&board[sr][sc].equals("O")&&board[sr-1][sc].equals("O")) {
//down
board[er][ec] = "O";
board[sr][sc] = " ";
board[sr-1][sc] = " ";
}
else {
System.out.println("Invalid move");
}
}
else {
//up
board[er][ec] = "O";
board[sr][sc] = " ";
board[sr+1][sc] = " ";
}
}
}
System.out.println("Would you like to play again?");
choice = GetInput();
if(choice.compareToIgnoreCase("Yes")==0) {
System.out.println("Yes was chosen");
}
else if(choice.compareToIgnoreCase("Y")==0) {
System.out.println("Yes was chosen");
}
else if(choice.compareToIgnoreCase("N")==0) {
System.out.println("No was chosen");
}
else if(choice.compareToIgnoreCase("No")==0) {
System.out.println("No was chosen");
}
else {
System.out.println("Invalid choice");
}
}
}
}