Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Java in General and the fly likes Trying to write a connect 4 game in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Trying to write a connect 4 game in java" Watch "Trying to write a connect 4 game in java" New topic

Trying to write a connect 4 game in java

Todd Smith Todd

Joined: Dec 11, 2002
Posts: 4
I am trying to write a Connect 4 board game for my Java Class I need to use at lease one multidimensional array to perform the task. I am totaly lost help.


* Intro to Java Programming CIPT 190
* Todd M. Smith
* Lab 12 Connect 4 Game
* This program is a simulator the for Connect Four game played by two people.
* Display the board and ask first user to select a column. Display the board
* again and ask the second user to select a column. After each user's turn you
* should check to see if that user has won.

public class connect4 {

public static void main(String[] args)
int MAX_ROWS = 6;
int MAX_COLS = 7;

typedef char game_board_t[MAX_ROWS][MAX_COLS];

int i, j;

game_board_t my_board;

for (i = 0; i < MAX_ROWS; i++)
for (j = 0; j < MAX_COLS; j++)
my_board[i][j] = 0;
BufferedReader dis = new BufferedReader(
new InputStreamReader(;

for (; {
System.out.println(" ");
System.out.println("select you move:");
try {
line = dis.readLine();
} catch (IOException e) {
Jimmy Die
Ranch Hand

Joined: Nov 20, 2003
Posts: 97

Well I've also been lost many times with java, so I can relate.

So far it looks like you've created your connect4 board. The next step I would take is to determine what is needed.

Well let's think about the game. How do you start to play? You put a chip in the slot.

So you have to first figure out how you determine which color chip to put in a slot (your array) and which slot?

For the chip you could prompt the user to enter in a chip color i.e. 'r' or 'b' or you could alternate between 'r' and 'b' from the code. I would prompt the user for chip color as it seems to me to be more life like.

Then you need to prompt the user for which column to put the chip into.

In my opinion these are the next steps you should consider focusing on. There are steps after this that you will also need to consider, but try to set up what I suggested first. Write the additional code out and update your specific question with your enhanced code, and if you have problems we'll try to help you solve them short of writing the program.


p.s. when you post code here there is a special way to do it so it looks like it is indented. Right next to the smiley faces there are UBB codes. Click on the one that reads 'CODE' and insert code between ...

Jimmy Die
Have you tried LearnNowOnline?
subject: Trying to write a connect 4 game in java