Results 1 to 4 of 4
- 02-06-2013, 07:43 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Need help with making connect 4 in eclipse without gui!!
Hey everyone, i am a newbie here to this forum. i have been working on a connect 4 as a school project and i am stuck; here is my code below so far can anyone help me out of this? thanks
import java.util.Scanner;
// This Program is used to design a game called Connect Four.
//this game has seven columns and six rows.
// the balls are two different colored RED and YELLOW.
// the balls can be any way possible straight, diagonal, vertical but anyhow four in a row
//
public class ConnectFour
{
static char [][] board= new char [6][7];
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.println("Welcome to Connect Four. ");// display message
System.out.println("Player 1 is RED and Player 2 is YELLOW. ");
int r= 0;// rows
int c= 0;//columns
short totalTurns = 0;// number of turns
char pCol;// Player color
//making a loop to make sure the number of turns given to players are less than 42
for (totalTurns = 0; totalTurns < 42; totalTurns++);
{ char Red = 0;
char Yellow = 0;
if (totalTurns %2 == 0)
pCol = Red;//red
else
pCol = Yellow;//yellow
// method for dropping a piece into a given- column
// dropping chips in the board
int availrow =0;
int col=-1;
while( col<0||col>6)
{
System.out.println("Enter the column ___ ");
col= input.nextInt();
if (board[5][col]==' ') availrow=5;
else if (board[4][col]==' ') availrow=4;
else if (board[3][col]==' ') availrow=3;
else if (board[2][col]==' ') availrow=2;
else if (board[1][col]==' ') availrow=1;
else if (board[0][col]==' ') availrow=0;
else
{ System.out.println("Invalid Column, Try Again. ");
}
col=-1;
displayBoard();// to call back board which is a main method and make sure the board is being displayed.
board[availrow][col] = 0;
}
{
while(totalTurns < 42){
if(winHorizontal() || winVertical() || windiagRight() || windiagLeft()){
System.out.println("Congratulations You win");
break;
}
}
}
}
}
// display the board
// make methods for displaying the board
public static void displayBoard()
{ System.out.println (" 0 1 2 3 4 5 6 ");
System.out.println("--------------");
for (int r=0;r<6;r++)
{
for (int c=0;c<7;c++)
{
System.out.print("|"+ board [r][c]);
}
System.out.println("|");
}
}
// make method for checking the board to see if there's a winning four ion a row pattern
// A horizontal win can start in any given row but but can only start in columns 0,1,2 or 3
// starting from any potential point, are are in the same color and aren't blank.
// all the possibilities are checked and NOT found four in column return false else return true.
public static boolean winHorizontal()
{
boolean winHorizontal = false;
for (int r=0; r<6;r++)// every row
{
for (int c=0;c<=3;c++)// columns 0-3 inclusive
{
if (board [r][c]==board [r][c+1]&&
board [r][c]==board [r][c+2]&&
board [r][c]==board [r][c+3]&&
board [r][c] !=' ') winHorizontal = true;
}
}
return winHorizontal;
}
// A vertical win can start in ANY column, but can only start in
// rows 0, 1, 2, or 3. From each potential starting point, see
// if the cell, plus the three below it, are all the same color
// and that the color isn't blank (i.e., must be red or yellow)
// If so, return true. If we've checked all possibilities and
// NOT found four in a row, return false.
//
public static boolean winVertical()
{
boolean winVertical = false;
for (int c=0; c<7; c++)// every column
{
for (int r=0; r<=3; r++)// rows 0-3 inclusive
{
if (board [r][c] == board [r+1][c] &&
board [r][c] == board [r+2][c] &&
board [r][c] == board [r+3][c] &&
board[r][c] != ' ') winVertical = true;
}
}
return winVertical;
}
// A diagonal right win can start in some particular rows and columns.
// since
public static boolean windiagRight()
{
boolean windiagRight =false;
for (int r=0 ;r<3 ; r++)// rows from 0-3
{
for (int c=0; c<6; c++)// columns 0-6
{
if(board [r][c] == board [r+1] [c+1] &&
board [r][c] == board [r+2][c+2] &&
board [r][c] == board [r+3][c+3] &&
board[r][c] != ' ') windiagRight =true;
}
}
return windiagRight;
}
//check diagonal left
public static boolean windiagLeft()
{
boolean windiagLeft =false;
for (int r=0 ;r>3 ; r++)
{
for (int c=3; c<7; c++)
{
if(board [r][c] == board [r+1] [c-1] &&
board [r][c] == board [r+2][c-2] &&
board [r][c] == board [r+3][c-3] &&
board[r][c] != ' ') windiagLeft =true;
}
}
return windiagLeft;
}
}
-
Re: Need help with making connect 4 in eclipse without gui!!
What is your question? All I see is code and some vague requirements. The more and better information that you can give about your problem the better the help we can give.
- 02-06-2013, 08:06 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Re: Need help with making connect 4 in eclipse without gui!!
The thing is i am unable to get the code running; the most it is able to do is display the message that welcome to the game and say the user that Player 1 is red and player 2 is yellow even ask them for input as well as display the blank table for the game but as soon as the user enters the column number that he wants to drop the chip in the program would just say invalid input and stop. The Question is how can i my my program to run without running into the above mentioned situation? thanks
- 02-06-2013, 10:28 PM #4
Senior Member
- Join Date
- Apr 2012
- Posts
- 129
- Rep Power
- 0
Re: Need help with making connect 4 in eclipse without gui!!
hello sunnybouy92
please edit your original post and surround your code with the code markers [code][\code] (but in capital letters).
Also, please post any stacktrace output you are getting showing us the error. or, can you describe to us where you are getting stuck? no one wants to do your homework for you, but we will surely help you if you help us help you.
Good luck!
Similar Threads
-
Can't debug in Eclipse(Can't connect to VM)
By huasen in forum New To JavaReplies: 8Last Post: 12-19-2010, 05:02 AM -
Eclipse Mysql connect
By jyotirmoy_deb in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-04-2010, 03:57 PM -
How do you connect to a MYSQL database from Eclipse?
By Menre in forum Advanced JavaReplies: 15Last Post: 04-15-2009, 01:51 AM -
Making Plugins for Eclipse
By javaplus in forum New To JavaReplies: 0Last Post: 12-17-2007, 08:31 AM -
Making connect 4 in java
By zoe in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks