nim game
import java.util.*;
public class Kitty01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("NIM game");
int n = 0;//determine position
int removechips = 0;
char chips[] = new char[32];
for (int i = 0; i < 32; i++) {
chips[i] = 'O';
}
n=printaboard(chips, n);
int player = 0;
while (player != 1 && player != 2) {
System.out.println("player 1 (1) player 2 (2)");
player = input.nextInt();
if (player != 1 && player != 2) {
System.out.println("enter 1 or 2");
continue;
}
}
switch (player) {
case 1:
playfirst(chips);
break;
case 2:
playnext(chips);
}
}
public static void playfirst(char chips[]) {
Scanner input = new Scanner(System.in);
int removechips;
int n = 0;
n=printaboard(chips, n);
while (n != 32) {
removechips = inputremovechip();//enter how many chip player wants to remove and cheak error
playerturn(n,removechips, chips);
n=printaboard(chips, n);
computerplay(chips, n);//computer play
n=printaboard(chips, n);
}
determinewinner(chips);
}
public static void playnext(char chips[]) {
int n=0;
int removechips;
while (n != 32) {
computerplay(chips, n);
n=printaboard(chips, n);
if (n == 32){ //end game
break;
}
removechips = inputremovechip();
playerturn(n,removechips, chips);
n=printaboard(chips, n);
if (n == 32){ //end game
break;
}
}
determinewinner(chips);
}
public static int inputremovechip() {
Scanner input = new Scanner(System.in);
int removechips = 0;
while (removechips != 1 && removechips != 2 && removechips != 3) {
System.out.println("How many chips you want to reamove? (1-3) ");
removechips = input.nextInt();
if (removechips != 1 && removechips != 2 && removechips != 3) {
cheakerror(removechips);
continue;
}
}
return removechips;
}
public static void cheakerror(int removechips) {
System.out.println("Only allow to remove 1-3");
}
public static char[] computerplay(char[] chips, int n) {
if ( n == 1 || n == 5
|| n == 9 || n == 13
|| n == 17 || n == 21
|| n == 25 || n == 29) {
for (int i = n; i < n + 3; i++) {
chips[i] = 'x';
}
} else if (n == 2 || n == 6
|| n == 10 || n == 14
|| n == 18 || n == 22
|| n == 26 || n == 30) {
for (int i = n; i < n + 2; i++) {
chips[i] = 'x';
}
} else if (n == 3 || n == 7
|| n == 11 || n == 15
|| n == 19 || n == 23
|| n == 27 || n == 31) {
for (int i = n; i < n + 1; i++) {
chips[i] = 'x';
}
}else {
for(int i = n; i < n + (int)(Math.random()*3)+1; i++) {
chips[i] = 'x';
}
}
return chips;
}
public static char[] playerturn(int n,int removechips,char chips[]){
for (int i = n; i < n + removechips; i++) {
chips[i] = 'X';//print'X'
}
return chips;
}
public static int printaboard(char chips[], int n) {
for (int k = 0; k < 32; k++) {
System.out.print(chips[k] + " ");
if (k == 7 || k == 15 || k == 23 || k == 31) {
System.out.println("");
}
if (chips[k]=='X'||chips[k]=='x'){
n=k+1;
}
}
System.out.println("+--------------------");//separate2 board
return n;
}
public static void determinewinner(char [] chips){
System.out.println("The winner is "+((chips[31] == 'X') ? "Player 1" : "Player 2"));
}
}
public class Kitty01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("NIM game");
int n = 0;//determine position
int removechips = 0;
char chips[] = new char[32];
for (int i = 0; i < 32; i++) {
chips[i] = 'O';
}
n=printaboard(chips, n);
int player = 0;
while (player != 1 && player != 2) {
System.out.println("player 1 (1) player 2 (2)");
player = input.nextInt();
if (player != 1 && player != 2) {
System.out.println("enter 1 or 2");
continue;
}
}
switch (player) {
case 1:
playfirst(chips);
break;
case 2:
playnext(chips);
}
}
public static void playfirst(char chips[]) {
Scanner input = new Scanner(System.in);
int removechips;
int n = 0;
n=printaboard(chips, n);
while (n != 32) {
removechips = inputremovechip();//enter how many chip player wants to remove and cheak error
playerturn(n,removechips, chips);
n=printaboard(chips, n);
computerplay(chips, n);//computer play
n=printaboard(chips, n);
}
determinewinner(chips);
}
public static void playnext(char chips[]) {
int n=0;
int removechips;
while (n != 32) {
computerplay(chips, n);
n=printaboard(chips, n);
if (n == 32){ //end game
break;
}
removechips = inputremovechip();
playerturn(n,removechips, chips);
n=printaboard(chips, n);
if (n == 32){ //end game
break;
}
}
determinewinner(chips);
}
public static int inputremovechip() {
Scanner input = new Scanner(System.in);
int removechips = 0;
while (removechips != 1 && removechips != 2 && removechips != 3) {
System.out.println("How many chips you want to reamove? (1-3) ");
removechips = input.nextInt();
if (removechips != 1 && removechips != 2 && removechips != 3) {
cheakerror(removechips);
continue;
}
}
return removechips;
}
public static void cheakerror(int removechips) {
System.out.println("Only allow to remove 1-3");
}
public static char[] computerplay(char[] chips, int n) {
if ( n == 1 || n == 5
|| n == 9 || n == 13
|| n == 17 || n == 21
|| n == 25 || n == 29) {
for (int i = n; i < n + 3; i++) {
chips[i] = 'x';
}
} else if (n == 2 || n == 6
|| n == 10 || n == 14
|| n == 18 || n == 22
|| n == 26 || n == 30) {
for (int i = n; i < n + 2; i++) {
chips[i] = 'x';
}
} else if (n == 3 || n == 7
|| n == 11 || n == 15
|| n == 19 || n == 23
|| n == 27 || n == 31) {
for (int i = n; i < n + 1; i++) {
chips[i] = 'x';
}
}else {
for(int i = n; i < n + (int)(Math.random()*3)+1; i++) {
chips[i] = 'x';
}
}
return chips;
}
public static char[] playerturn(int n,int removechips,char chips[]){
for (int i = n; i < n + removechips; i++) {
chips[i] = 'X';//print'X'
}
return chips;
}
public static int printaboard(char chips[], int n) {
for (int k = 0; k < 32; k++) {
System.out.print(chips[k] + " ");
if (k == 7 || k == 15 || k == 23 || k == 31) {
System.out.println("");
}
if (chips[k]=='X'||chips[k]=='x'){
n=k+1;
}
}
System.out.println("+--------------------");//separate2 board
return n;
}
public static void determinewinner(char [] chips){
System.out.println("The winner is "+((chips[31] == 'X') ? "Player 1" : "Player 2"));
}
}