Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import static java.lang.System.*;
- class Main
- {
- public static void main(String[] args)
- {
- GameServer server = new GameServer(new HumanPlayer(), new ComputerPlayer(2));
- server.play();
- out.println(server.whoWon());
- }
- }
- class BoardConfiguration
- {
- public static final String ANSI_RESET = "\u001B[0m";
- public static final String ANSI_BLACK = "\u001B[30m";
- public static final String ANSI_RED = "\u001B[31m";
- public static final String ANSI_GREEN = "\u001B[32m";
- public static final String ANSI_YELLOW = "\u001B[33m";
- public static final String ANSI_BLUE = "\u001B[34m";
- public static final String ANSI_PURPLE = "\u001B[35m";
- public static final String ANSI_CYAN = "\u001B[36m";
- public static final String ANSI_WHITE = "\u001B[37m";
- private byte B[];
- private int Turn;
- private boolean Empty;
- int Value;
- public BoardConfiguration()
- {
- B = new byte[14];
- for (int i = 0; i < 6; i++)
- B[i] = 6;
- for (int i = 7; i < 13; i++)
- B[i] = 6;
- Turn = 0;
- Empty = false;
- }
- public boolean move( int bowl )
- {
- int inhalt;
- int h;
- boolean kalah = false;
- if (Turn == 0) // Player1
- {
- h = bowl;
- inhalt = B[h];
- B[h] = 0;
- while (inhalt > 0)
- {
- h++;
- if (h >= 13)
- h = 0;
- B[h]++;
- inhalt--;
- }
- if (h < 6 && B[h] == 1)
- { B[h] = 0; B[6]++; B[6] += B[12 - h]; B[12 - h] = 0; }
- if (h != 6)
- Turn = 1;
- else
- kalah = true;
- }
- else // Player2
- {
- h = 7 + bowl;
- inhalt = B[h];
- B[h] = 0;
- while (inhalt > 0)
- {
- h++;
- if (h == 6)
- h=7;
- if (h >= 14)
- h=0;
- B[h]++;
- inhalt--;
- }
- if (h > 6 && h < 13 && B[h] == 1)
- { B[h] = 0; B[13]++; B[13] += B[12 - h]; B[12 - h] = 0; }
- if (h != 13)
- Turn = 0;
- else
- kalah = true;
- }
- // Emptyness test
- // Player1
- Empty = true;
- for (h = 0; h < 6; h++)
- if (B[h] != 0)
- {
- Empty = false;
- break;
- }
- if (Empty)
- {
- for (h = 7; h < 13; h++)
- { B[13] += B[h]; B[h] = 0; }
- return kalah;
- }
- // Player2
- Empty = true;
- for (h = 7; h < 13; h++)
- if (B[h] != 0)
- {
- Empty = false;
- break;
- }
- if (Empty)
- for (h = 0; h < 6; h++)
- { B[6] += B[h]; B[h] = 0; }
- return kalah;
- }
- private void print1()
- {
- for (int i = 0; i < 6; i++)
- out.print(B[i] + " ");
- out.println(ANSI_RED + B[6] + " " + ANSI_RESET);
- }
- private void print2()
- {
- for (int i = 7; i < 13; i++)
- out.print(B[i] + " ");
- out.println(ANSI_GREEN + B[13] + " " + ANSI_RESET);
- }
- public void print()
- {
- out.println("*******************");
- if (Turn == 0)
- { out.println("Now it's your turn, " + ANSI_RED + "player1" + ANSI_RESET); print2(); print1(); }
- else
- { out.println("Now it's your turn, " + ANSI_GREEN + "player2" + ANSI_RESET); print1(); print2(); }
- out.println("*******************");
- }
- public boolean makeMove( int bowl ) throws RuntimeException
- {
- if (bowl < 0 || bowl > 6)
- throw new RuntimeException("CHEATER!!!!!!!!111!!1!!");
- return move(bowl);
- }
- public int getMyBowl( int bowl, int playerNum )
- {
- if (bowl < 0 || bowl > 6 || playerNum < 1 || playerNum > 2)
- return -1;
- return B[bowl + 7 * --playerNum];
- }
- }
- interface Player
- {
- public boolean makeMove( BoardConfiguration board );
- }
- class HumanPlayer implements Player
- {
- static Scanner inputStream = new Scanner(in);
- public boolean makeMove( BoardConfiguration board )
- {
- int bowl;
- while (true)
- {
- if (inputStream.hasNextInt())
- {
- bowl = inputStream.nextInt();
- if (bowl <= 6 && bowl >= 0)
- break;
- }
- out.println("Incorrect input!!");
- }
- return board.makeMove(bowl);
- }
- }
- class ComputerPlayer implements Player
- {
- int myNum = 0;
- public ComputerPlayer( int num )
- {
- myNum = num;
- }
- public boolean makeMove( BoardConfiguration board )
- {
- int max = 0, bowl = 0, tmp;
- for (int i = 0; i < 6; i++)
- {
- tmp = board.getMyBowl(i, myNum);
- if (max < tmp)
- {
- max = tmp;
- bowl = i;
- }
- }
- out.println("I choose " + bowl);
- return board.makeMove(bowl);
- }
- }
- class GameServer
- {
- private Player player1;
- private Player player2;
- private BoardConfiguration board;
- private int winner;
- public GameServer(Player pl1, Player pl2)
- {
- player1 = pl1;
- player2 = pl2;
- board = new BoardConfiguration();
- }
- public int play()
- {
- while (true)
- {
- board.print();
- if (player1.makeMove(board))
- {
- winner = 1;
- break;
- }
- board.print();
- if (player2.makeMove(board))
- {
- winner = 2;
- break;
- }
- }
- return winner;
- }
- public String whoWon()
- {
- return "Player" + winner + " won!!!";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement