Connect4


Collapse Content

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).

enter image description here 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

  • It 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[][]?

  • You 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..)

  • Am I placing 1 or 2?

  • I 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.

    cont...
  • I found the compile problem. A change I made in the method call disappeared for some reason and I had to reenter it.

  • I 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.

  • Is 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!

  • @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.

  • For 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.

  • I 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]

    cont...
  • @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.

  • Oops! I wrote the code with the col & row reversed! Thanks!

  • Not 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

    cont...
  • is the goal to write the program? is the goal to find winning moves? wat is the goal what is given?

  • You 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.

  • I am sorry but i still dont understand?

  • It'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.

  • I think the links is wrong, cause that is what i tried to see what would happen.

Contact Us
Sign in or email us at [email protected]