# 2-D java array question

Page 1 of 1

## 1 Replies - 6417 Views - Last Post: 08 August 2012 - 12:00 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288268&amp;s=7f7c6c04313da58424cc02ec77235b51&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 htth07

Reputation: 0
• Posts: 1
• Joined: 07-August 12

# 2-D java array question

Posted 07 August 2012 - 11:44 PM

I am a novice at programming in Java. Here is a question which I am stuck at...

Write a program to simulate a simplified version of Connect 4. Connect 4 is a two player game played on a 6x6 game board, where the objective is to drop circular pieces into 6 column slots. The winner is decided when one of the two computer players is able to get 4 circular pieces together horizontally or vertically. This modified version of Connect 4 will be simulated using two computer players.

Assume there is no user input for this game. The 6x6 board will be simulated using a 6 x 6 2D array. At the start, the game must randomly decide which computer player (1 or 2) will go first. Then, the program should simulate the game play as follows:

• At each turn, the current state of the game board should be displayed
• At each turn, each computer player randomly drops a circular piece into one of the 6 column slots. The column to be dropped into must be randomly decided. Each column slot can only take a maximum of 6 pieces. Then, when a circular piece is dropped in, it will go into a position in that column, which has a x and y value (e.g. [2][2]). If that column is full, then the computer player skips the turn.
• After each circular piece is dropped, the program must check if that computer player has won or not. A win means that after that drop there is either a horizontal or vertical group of 4 circular pieces on the game board.
• If there is no winner, then at the end of the turn, the updated state of the 6x6 2D game board should be displayed.

Overall, each position (or slot) in the displayed 6x6 2D game board can contain any of the following:
• -1 not occupied yet
• 1 occupied by computer player 1’s circular piece
• 2 occupied by computer player 2’s circular piece

The game ends when one of the following conditions is satisfied:
• Either computer player 1 or 2 has a horizontal or vertical group of 4 pieces on the game board. If this condition is satisfied the program should print that computer player as the winner.
• There are no more slots in the game board to drop pieces onto. Then, the program should print “No winner” as output.

Your program must, at least, include the following methods/functions with the correct input parameters and output type:

i. initializeGameBoard, which will take as input the player’s 2D array and assign blank slots containing -1 (-1 is assumed to be unoccupied yet).

ii. dropPiece, which will drop a circular piece randomly into a slot in one of the six columns.

Your program also needs to track and print the following:
• Number of pieces dropped into each column by computer player 1
• Number of pieces dropped into each column by computer player 2

Here is my code:

```package assignment_qn6;

/**
*
* @author Henry
*/
public class Assignment_Qn6 {

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
int [][]game=new int[6][6];
initializeGameBoard(game);

int playerStart=0;
playerStart=(int)((Math.random()*2)+1);

}

public static void initializeGameBoard(int [][]x)
{
for(int i=0; i<6;i++)
{
for(int k=0;k<6;k++)
{
x[i][k]=-1;
}
}
}

public static int[][] dropPiece(int [][]x,int y)
{
int randCol=0;
randCol=(int)((Math.random()*6)+1);
for(int i=0;i<6;i++)
{

x[i][k]=[randCol][y]; //This is where I've stopped...

}
}
}

```

Is there any advice or direction I could take like using if-else statements to drop the piece into the random column?

This post has been edited by jon.kiparsky: 07 August 2012 - 11:55 PM
Reason for edit:: added code tags - please use them in the future!

Is This A Good Question/Topic? 0

## Replies To: 2-D java array question

### #2 jon.kiparsky

• Beginner

Reputation: 11041
• Posts: 18,857
• Joined: 19-March 11

## Re: 2-D java array question

Posted 08 August 2012 - 12:00 AM

It's not clear to me what it is you're asking for. "Advice" is hard to give when you've really written nothing.
If you have a particular question, please ask it. If not, you'll need to write some more code before there's anything to really advise you on.