Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package blatt8;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- public class ConnectFour {
- private static final char[] players = new char[]{'X', 'O'};
- private final int w, h;
- private final char[][] grid;
- private int lastCol = -1, lastTop = -1;
- public ConnectFour(int w, int h){
- this.w = w;
- this.h = h;
- this.grid = new char[h][];
- for(int i = 0; i < h; h++){
- Arrays.fill(this.grid[i] = new char[w],'.');
- }
- }
- public String toString(){
- return IntStream.range(0, this.w)
- .mapToObj(Integer::toString)
- .collect(Collectors.joining()) + "\n" +
- Arrays.stream(this.grid)
- .map(String::new)
- .collect(Collectors.joining("\n")); //error points here
- }
- public void selectAndPlace(char symbol, Scanner input){
- do{
- System.out.println("\nPlayer"+symbol+"turn: ");
- int column = input.nextInt();
- if(!(0<= column && column < this.w)){
- System.out.println("Column must be between 0 and "+ (this.w - 1));
- continue;
- }
- for(int j = this.h - 1; j >= 0; j--){
- if(this.grid[j][column] == '.'){
- this.grid[this.lastTop = j][this.lastCol=column] = symbol;
- return;
- }
- }
- } while (true);
- }
- public boolean isWinningMove(){
- if(this.lastCol == -1){
- throw new IllegalStateException("no move has been made yet");
- }
- char symmetry = this.grid[this.lastTop][this.lastCol];
- String streak = String.format("%c%c%c%c", symmetry, symmetry, symmetry, symmetry);
- return contains(this.horizontal(), streak) ||
- contains(this.vertical(), streak) ||
- contains(this.slashDiagonal(), streak) ||
- contains(this.backslashDiagonal(),streak);
- }
- private String horizontal(){
- return new String(this.grid[this.lastTop]);
- }
- private String vertical(){
- StringBuilder sb = new StringBuilder(this.h);
- for(int i = 0; i < this.h; i++){
- sb.append(this.grid[i][this.lastCol]);
- }
- return sb.toString();
- }
- private String slashDiagonal(){
- StringBuilder sb = new StringBuilder(this.h);
- for(int i = 0; i < this.h; i++){
- int j = this.lastCol + this.lastTop - i;
- if(0<= j && j < this.w){
- sb.append(this.grid[i][j]);
- }
- }
- return sb.toString();
- }
- private String backslashDiagonal(){
- StringBuilder sb = new StringBuilder(this.h);
- for(int i = 0; i < this.h; i++){
- int j = this.lastCol - this.lastTop + i;
- if(0<= j && j < this.w){
- sb.append(this.grid[i][j]);
- }
- }
- return sb.toString();
- }
- private static boolean contains(String hays, String string){
- return hays.indexOf(string) >= 0;
- }
- public static void main(String[] args){
- try (Scanner input = new Scanner(System.in)){
- int h = 6, w = 8, moves = h * w;
- ConnectFour board = new ConnectFour(w,h);
- System.out.println("Choose column between 0" + " and " + (w-1));
- System.out.println(board);
- for(int player = 0; moves-- > 0; player = 1 - player){
- char symbol = players[player];
- board.selectAndPlace(symbol, input);
- System.out.println(board);
- if(board.isWinningMove()){
- System.out.println("Player " + symbol + " wins!");
- return;
- }
- }
- System.out.println("No winner, game over");
- } catch (NullPointerException exp){
- System.out.println("null value found!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment