Connect4
Connect 4 is a popular game where the goal is to connect 4 pieces vertically, horizontally or diagonally. Each turn, the current player drops a piece into 1 of 7 columns on the game board, and the piece falls to the bottom. The game ends when a player connects 4 pieces (and wins) or when the board is filled up (a tie).
from Wikipedia
Challenge
Create a program to Play Connect 4!
Details
Each turn, you will be be given a method play
that takes in board
, a double array of integers, which contains the current game board. You must return an integer from 0 to 6 with the column you want to go in for the current move.
The Game Board
The board contains the column (or x position) and then the row (or y position):
board[column][row]
Columns 0 1 2 3 4 5 6 0 . . . . . . . r 1 . . . . . . . o 2 . . . . . . . w 3 . . . . . . . s 4 . . . . . . . 5 . . . . . . .
Empty cells are represented by 0 in the array. X is represented by 1 and O by 2. In the position below, X played column 3
to occupy position board[3][5]
, and O played column 4 to occupy board[4][5]
:
0 1 2 3 4 5 6 --------------- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 0 0
Challenge
Create a program to play Connect4. Finish the method play
so it returns an integer from 0 to 6 every move. You can create other methods and instance variables too.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Kendall Ponder
Jun 2, 10:26 PMIt is not clear to me what is given and what we are supposed to program. Is the play method used by both players? I Am I supposed to use play to update board[][]?
Learneroo
Jun 2, 10:29 PMYou are just programming one side in the game, and need to return an integer from 0 to 6 for the column you're moving in. (Other code will then perform your move and do another move, and then call your play method again..)
Kendall Ponder
Jun 3, 8:23 AMThanks!
Kendall Ponder
Jun 9, 12:48 PMAm I placing 1 or 2?
Kendall Ponder
Jun 9, 1:05 PMI developed my code with BlueJ and got it to compile and got a runtime array out of bounds error. I fixed the problem but now I get a compile error saying I can't return my integer method.
I haven't changed the structure of the method (all I did was change the order of a while test in the method) so why did it compile before and not now?
Kendall Ponder
Jun 9, 1:08 PMI found the compile problem. A change I made in the method call disappeared for some reason and I had to reenter it.
Kendall Ponder
Jun 9, 1:12 PMI don't see how I can find an array out of bounds problem when I can't see all of the code. The numbers of the runtime error don't match the numbers of my code.
Kendall Ponder
Jun 10, 4:43 PMIs there a way to print so I can see what certain values are before the program crashes. I tried System.out.println() and I don't get anything. Thanks!
Learneroo
Jun 10, 6:31 PM@Kendall, as mentioned, you can see what line play() is called in the error to see how many lines of code is being added. So if play() is called 338 when it's really line 4, that means you just need to subtract 334 lines from any error line.
Learneroo
Jun 10, 6:33 PMFor seeing errors, you can always use the error output to print to the error box. In Java, just use
System.err.println
to print to the error output.Kendall Ponder
Jun 10, 7:14 PMThanks!
Kendall Ponder
Jun 10, 7:19 PMI thought in the following code:
while(row<6 && board[row][col]!=0){
row++;
}
The row<6 would be evaluated first so if row=6 the board[row][col]
would not be evaluated and cause an array out of bounds. It seemed to work when I ran the code in BlueJ. Am I mistaken? Thanks!
Learneroo
Jun 10, 8:08 PM@kendall, that's correct, but maybe it's col that's out of bounds? Also make sure you're checking the right ones, since the format I mentioned is
board[col][row]
, not [row][col]. You can also use the included helper method to check if a move is legal.Kendall Ponder
Jun 10, 10:44 PMOops! I wrote the code with the col & row reversed! Thanks!
Kendall Ponder
Jun 13, 11:43 AMNot a question but I find how the editor highlights entire words when you click on the code annoying. I have to click three times to insert something and have accidently deleted code several
times because I have never had an editor which did not just put the cursor where you click on a single click.
thales
Jul 14, 6:02 AMis the goal to write the program? is the goal to find winning moves? wat is the goal what is given?
Learneroo
Jul 14, 9:43 AMYou need to write an AI/program/bot that plays connect4, but doesn't run the game itself. All it does is take in a double-array board each turn and return an integer move.
thales
Jul 14, 9:50 AMI am sorry but i still dont understand?
Learneroo
Jul 14, 10:06 AMIt's the same idea as the crossoff game. Here's a simple program that plays legally but loses. The goal is to improve your program's moves so it wins.
thales
Jul 14, 11:56 AMI think the links is wrong, cause that is what i tried to see what would happen.