morry2341

ConnectFour

Jan 9th, 2023
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. package blatt8;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6. import java.util.stream.IntStream;
  7.  
  8. public class ConnectFour {
  9.     private static final char[] players = new char[]{'X', 'O'};
  10.     private final int w, h;
  11.     private final char[][] grid;
  12.     private int lastCol = -1, lastTop = -1;
  13.  
  14.     public ConnectFour(int w, int h){
  15.         this.w = w;
  16.         this.h = h;
  17.         this.grid = new char[h][];
  18.         for(int i = 0; i < h; h++){
  19.             Arrays.fill(this.grid[i] = new char[w],'.');
  20.         }
  21.     }
  22.  
  23.     public String toString(){
  24.         return IntStream.range(0, this.w)
  25.                         .mapToObj(Integer::toString)
  26.                         .collect(Collectors.joining()) + "\n" +
  27.                Arrays.stream(this.grid)
  28.                         .map(String::new)
  29.                         .collect(Collectors.joining("\n")); //error points here
  30.     }
  31.  
  32.     public void selectAndPlace(char symbol, Scanner input){
  33.         do{
  34.             System.out.println("\nPlayer"+symbol+"turn: ");
  35.             int column = input.nextInt();
  36.             if(!(0<= column && column < this.w)){
  37.                 System.out.println("Column must be between 0 and "+ (this.w - 1));
  38.                 continue;
  39.             }
  40.             for(int j = this.h - 1; j >= 0; j--){
  41.                 if(this.grid[j][column] == '.'){
  42.                     this.grid[this.lastTop = j][this.lastCol=column] = symbol;
  43.                     return;
  44.                 }
  45.             }
  46.         } while (true);
  47.     }
  48.  
  49.     public boolean isWinningMove(){
  50.         if(this.lastCol == -1){
  51.             throw new IllegalStateException("no move has been made yet");
  52.         }
  53.         char symmetry = this.grid[this.lastTop][this.lastCol];
  54.         String streak = String.format("%c%c%c%c", symmetry, symmetry, symmetry, symmetry);
  55.         return contains(this.horizontal(), streak) ||
  56.                contains(this.vertical(), streak) ||
  57.                contains(this.slashDiagonal(), streak) ||
  58.                contains(this.backslashDiagonal(),streak);
  59.     }
  60.  
  61.     private String horizontal(){
  62.         return new String(this.grid[this.lastTop]);
  63.     }
  64.  
  65.     private String vertical(){
  66.         StringBuilder sb = new StringBuilder(this.h);
  67.         for(int i = 0; i < this.h; i++){
  68.             sb.append(this.grid[i][this.lastCol]);
  69.         }
  70.         return sb.toString();
  71.     }
  72.  
  73.     private String slashDiagonal(){
  74.         StringBuilder sb = new StringBuilder(this.h);
  75.         for(int i = 0; i < this.h; i++){
  76.             int j = this.lastCol + this.lastTop - i;
  77.             if(0<= j && j < this.w){
  78.                 sb.append(this.grid[i][j]);
  79.             }
  80.         }
  81.         return sb.toString();
  82.     }
  83.  
  84.     private String backslashDiagonal(){
  85.         StringBuilder sb = new StringBuilder(this.h);
  86.         for(int i = 0; i < this.h; i++){
  87.             int j = this.lastCol - this.lastTop + i;
  88.             if(0<= j && j < this.w){
  89.                 sb.append(this.grid[i][j]);
  90.             }
  91.         }
  92.         return sb.toString();
  93.     }
  94.  
  95.     private static boolean contains(String hays, String string){
  96.         return hays.indexOf(string) >= 0;
  97.     }
  98.  
  99.     public static void main(String[] args){
  100.         try (Scanner input = new Scanner(System.in)){
  101.             int h = 6, w = 8, moves = h * w;
  102.             ConnectFour board = new ConnectFour(w,h);
  103.             System.out.println("Choose column between 0" + " and " + (w-1));
  104.             System.out.println(board);
  105.  
  106.             for(int player = 0; moves-- > 0; player = 1 - player){
  107.                 char symbol = players[player];
  108.                 board.selectAndPlace(symbol, input);
  109.                 System.out.println(board);
  110.                 if(board.isWinningMove()){
  111.                     System.out.println("Player " + symbol + " wins!");
  112.                     return;
  113.                 }
  114.             }
  115.             System.out.println("No winner, game over");
  116.         } catch (NullPointerException exp){
  117.             System.out.println("null value found!");
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment