Advertisement
Guest User

Untitled

a guest
May 6th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. public interface Player {
  2.  
  3.     public String getUserInput(Board Connect4) throws IOException;
  4.     public boolean hasWon(Board Connect4);
  5. }
  6.  
  7. public class HumanPlayer implements Player {
  8.     private int boardColumn = 0;
  9.  
  10.     @Override
  11.     public String getUserInput(Board Connect4) throws IOException {
  12.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  13.             //Player One Logic ----------------------------------------------------------------
  14.  
  15.             while (true) {
  16.                 System.out.println();
  17.                 System.out.println("Player 1, please select your column:");
  18.                 boardColumn = Integer.parseInt(br.readLine());
  19.  
  20.                 if (Connect4.canMakeMove(boardColumn)) {
  21.  
  22.                     break;
  23.                 } else
  24.                     System.out.println("Column " + boardColumn + " is already full!!");
  25.             }
  26.         return null;
  27.     }
  28.  
  29.     @Override
  30.     public boolean hasWon(Board Connect4) {
  31.         if(Connect4.placeCounter(boardColumn, 1)){
  32.             Connect4.printBoard();
  33.             System.out.println("");
  34.             System.out.println("Congratulations! Player 1 has won the game");
  35.             return true;
  36.         }
  37.         return false;
  38.     }
  39. }
  40.  
  41. public class ComputerPlayer implements Player{
  42.     private int boardColumn = 0;
  43.  
  44.     @Override
  45.     public String getUserInput(Board Connect4) throws IOException {
  46.  
  47.         outer:
  48.         while(true){
  49.             System.out.println("");
  50.             System.out.println("The Computer has selected a column and played a counter");
  51.             System.out.println("");
  52.  
  53.             Random r = new Random();
  54.             int num = r.nextInt(7);
  55.  
  56.             boardColumn = num;
  57.  
  58.             if(Connect4.canMakeMove(boardColumn)){
  59.  
  60.                 break;
  61.             }
  62.             else
  63.                 System.out.println("Column "+boardColumn+" is already full!!");
  64.         }
  65.  
  66.         return null;
  67.     }
  68.  
  69.     @Override
  70.     public boolean hasWon(Board Connect4) {
  71.         if(Connect4.placeCounter(boardColumn, 2)){
  72.             Connect4.printBoard();
  73.             System.out.println("");
  74.             System.out.println("Unlucky! The Computer has won this game");
  75.             return true;
  76.         }
  77.         return false;
  78.     }
  79. }
  80.  
  81. public class Main {
  82.  
  83.     public static void main(String args[])throws IOException{
  84.         Board Connect4 = new Board();
  85.  
  86.  
  87.         HumanPlayer human = new HumanPlayer();
  88.         ComputerPlayer computer = new ComputerPlayer();
  89.  
  90.         while(!human.hasWon(Connect4) && !computer.hasWon(Connect4)) {
  91.             Connect4.printBoard();
  92.             human.getUserInput(Connect4);
  93.             computer.getUserInput(Connect4);
  94.         }
  95.     }
  96.  
  97.  
  98.     public static void welcomeMessage() {
  99.         System.out.println("Welcome to Connect 4");
  100.         System.out.println("There are 2 players red and yellow");
  101.         System.out.println("Player 1 is Red, Player 2 is Yellow");
  102.         System.out.println("To play the game type in the number of the boardColumn you want to drop you counter in");
  103.         System.out.println("A player wins by connecting 4 counters in a row - vertically, horizontally or diagonally");
  104.         System.out.println("");
  105.     }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement