Click here to download the IntelliJ project for this exercise. Alternatively, you can find all the exercices on this git repository
This exercise is already available in your IntelliJ as a project.
What you need to do is described in the comments at the top of the file in src/main/java/
.
package basics; /** * A class that represents a game of Four in a Row. * The game is played on a 6x7 board. * A player wins when he has 4 pieces in a row, column or diagonal. * * ForInARow is a two-player connection rack game, in which the players choose a color and * then take turns dropping colored tokens into a six-row, seven-column vertically suspended grid. * The pieces fall straight down, occupying the lowest available space within the column. * * The objective of the game is to be the first to form a horizontal, * vertical, or diagonal line of four of one's own tokens. * * Your taks is to model the game and implement the method hasWon(char player) that returns true. * if the player has won the game. * We advise you to model the state of the game with an internal 2D array of char. */ public class FourInARow { private static final int ROWS = 6; private static final int COLUMNS = 7; private static final char EMPTY = '-'; private static final char[] PLAYERS = {'X', 'O'}; // add your own instance variables here public FourInARow() { // add your own code here } /** * Play a piece in column j for the given player. * @param j the column index * @param player the player (X or O) * @throws IllegalArgumentException if j is not a valid column index or if the column is full or if the player is not X or O */ public void play(int j, char player) { // add your own code here } /** * Returns true if the player has won the game. * @param player the player (X or O) * @return true if the player has won the game * @throws IllegalArgumentException if the player is not X or O */ public boolean hasWon(char player) { // add your own code here return false; } }
- Instruction provided at the top of the source file on IntelliJ.
- Debug using small and easy unit tests provided in junit tests, it can also help to clarify the instructions.