We have to make a ConnectFour game in java for a project. I think I have managed to code most of it properly (Let me know if this is wrong). I''m having trouble figuering out how to decide who wins. I know you can win connect 4 horizontally, vertically, and diagonally( if the board size allows it).
Here's the link for reference: CMSC 202 - Spring 2013 - Project 2
Here's my ConnectFour.java class
package proj2; public class ConnectFour { private String player1 = ""; private String player2 = ""; private int boardArea = 0; private int playerPieces = 0; private boolean turn = true; private int rows = 0; private int columns = 0; private int spot = 0; Move[][] board = new Move [rows][columns]; public ConnectFour( int rows, int columns, String player1, String player2 ) { this.rows = rows; this.columns = columns; this.player1 = player1; this.player2 = player2; for(int i = 0; i < rows; i++) { for( int j = 0; j < columns ; j++) { board[i][j] = Move.EMPTY; } } } public int getBoardArea() { boardArea = rows * columns; return boardArea; } public void turn(int columns, boolean playerTurn) {//sorts through the column by rows. Starting at the top row. if(board[0][columns] != Move.EMPTY) { throw new RuntimeException("Columns is full. Try again."); } for(int i = 0; i < rows ; i++ ) { if(board[i][columns] != Move.EMPTY) { spot = i; } } if(playerTurn == true)//if it's player ones turn place piece //on the board { board[spot][columns] = Move.PLAYER1; } else if(playerTurn == false)//if it's player twos turn place piece //on the board { board[spot][columns] = Move.PLAYER2; } } public void setBoardArea() { //if(boardArea ) } public void setPieces(int redPieces, int blackPieces ) { //sets red and black pieces to players. P1 gets red P2 gets black. playerPieces = redPieces; playerPieces = blackPieces; } public void winCondition()//checks if a player has won { } }
Here's my Enum Move
package proj2; public enum Move { PLAYER1, PLAYER2, EMPTY }
Here's my Project2.java class
package proj2; import java.util.Scanner; public class Project2 { /** * This class drives the Connect Four game. * @version 3/10/2013 * @author Nathan Graddick <grad1@umbc.edu> * @project CMSC 202 - Spring 2013 - Project 2 */ public static void main(String[] args) { // TODO Auto-generated method stub String selection = ""; String player1 = ""; String player2 = ""; int rows = 0; int columns = 0; boolean move = true; Scanner scan = new Scanner(System.in); System.out.println("Player1: Enter your name:"); player1 = scan.nextLine(); System.out.println("Player2: Enter your name:"); player2 = scan.nextLine(); System.out.println("How many rows do you want?"); rows = scan.nextInt(); System.out.println("How many columns do you want?"); columns = scan.nextInt(); ConnectFour board = new ConnectFour(rows, columns, player1, player2); System.out.println("The area of the board is: " + board.getBoardArea()); do { /*for(int i = 0; i < rows; i++) { board[i][i] = scan.nextLine(); for(int j = 0; i < columns; i++) { board[j][j] = scan.nextLine(); } System.out.println(columns); } System.out.println(rows);*/ }while(!selection.equalsIgnoreCase("Q"));//exits the do while loop } }