Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Connect Four Game need help pleaseee!!

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Connect Four Game need help pleaseee!!

    I have to make a processing game, I got my game to fully work but I want to add some motion into it I want to have the chips when clicked to start on the top of the board and drop down to the bottom. Please help me!!!

    int count = 0;
    int rows = 6;
    int cols = 7;
    int winner = 0;
    CircleButton[][] grid = new CircleButton[cols][rows];
    int[][] gridChecker = new int[cols][rows];
    boolean locked = false;
    boolean gameOn = true;

    void setup()
    {
    smooth();
    for (int a = 0; a<cols; a++) {
    for (int b = 0; b<rows; b++) {
    gridChecker[a][b] = 10;
    }
    }
    int a;
    int b;
    for ( a = 0; a<cols; a++) {
    for (b = 0; b<rows; b++) {
    grid[a][b] = new CircleButton(100*a + 50, 100*b + 50, 80, color(255), color(153));
    }
    }

    color buttoncolor = color(204);
    color highlight = color(153);
    ellipseMode(CENTER);

    size(700, 600);
    background(255, 204, 0);


    stroke(126);
    for ( int c = 1; c <= cols; c++) {
    line( c*(width/cols), 0, c*(width/cols), height);
    }
    for (int c = 1; c<=rows; c++) {
    line(0, c*(height/rows), width, c*(height/rows));
    }
    }
    void draw()
    {
    update(mouseX, mouseY);
    for (int c = 0; c<cols; c++) {
    for (int d = 0; d<rows; d++) {
    grid[c][d].display();
    }
    }
    if (checkWin() == true) {
    gameOn = false;
    PFont font = loadFont("IskoolaPota-48.vlw");
    textFont(font, 80);
    if (winner == 4) {
    shadowtext("Red Wins!", width/4, height/2, 3);
    }
    else if(winner == 8) {
    shadowtext("Black Wins!", width/3.5, height/2, 3);
    }
    shadowtext("Click to play again", width/5.5, height*.3, 1);
    }

    }
    void shadowtext (String s, float x, float y, int o) {
    fill(100,100);
    text(s, x+o, y+o);
    fill(58,12,247);
    text(s, x, y);
    }
    void mousePressed() {
    if(gameOn == false) {
    gameOn = true;
    setup();
    }
    }

    boolean checkWin()
    {

    int counter;
    //horizontal
    for (int a=0; a < rows; a++) {
    for (int b=0; b < cols-3; b++) {
    int tCheck = (gridChecker[b][a]) + (gridChecker[b+1][a]) + (gridChecker[b+2][a]) + (gridChecker[b+3][a]);
    if (tCheck == 8 || tCheck == 4)
    {
    winner = tCheck;
    return true;
    }
    }
    }


    //vertical
    for (int a=0; a < rows-3; a++) {
    for (int b=0; b < cols; b++) {
    int tCheck = (gridChecker[b][a]) + (gridChecker[b][a+1]) + (gridChecker[b][a+2]) + (gridChecker[b][a+3]);
    if (tCheck == 8 || tCheck == 4)
    {
    winner = tCheck;
    return true;
    }
    }
    }


    //diagonals
    for (int a=0; a < rows-3; a++) {
    for (int b=0; b < cols-3; b++) {
    int tCheck = (gridChecker[b][a]) + (gridChecker[b+1][a+1]) + (gridChecker[b+2][a+2]) + (gridChecker[b+3][a+3]);
    if (tCheck == 8 || tCheck == 4)
    {
    winner = tCheck;
    return true;
    }
    }
    }


    for (int a =0; a < rows-3; a++) {
    for (int b=0; b < cols-3; b++) {
    int tCheck = (gridChecker[b][a]) + (gridChecker[b+1][a+1]) + (gridChecker[b+2][a+2]) + (gridChecker[b+3][a+3]);
    if (tCheck == 8 || tCheck == 4)
    {
    winner = tCheck;
    return true;
    }
    }
    }

    for (int a=0; a < rows-3; a++) {
    for (int b=3; b < cols; b++) {
    int tCheck = (gridChecker[b][a]) + (gridChecker[b-1][a+1]) + (gridChecker[b-2][a+2]) + (gridChecker[b-3][a+3]);
    if (tCheck == 8 || tCheck == 4)
    {
    winner = tCheck;
    return true;
    }
    }
    }

    return false;
    }



    void update(int x, int y)
    {

    if (mousePressed && gameOn) {
    for (int c = 0; c<cols; c++) {
    for (int d = 0; d<rows; d++) {
    if (grid[c][d].pressed() && (grid[c][d].getColor() == color(255)|| grid[c][d].getColor() == color(153)) ) {
    if (d == 5 || (gridChecker[c][d + 1] == 1 || gridChecker[c][d+1] == 2)) {
    grid[c][d].setHighlight();
    count++;
    if (count % 2 == 0) {
    grid[c][d].setColor(color(255, 0, 0));
    gridChecker[c][d] = 1;
    }
    else {
    grid[c][d].setColor(color(0));
    gridChecker[c][d] = 2;
    }
    }
    }
    }
    }
    }
    if (locked == false && gameOn) {
    for (int c = 0; c<cols; c++) {
    for (int d = 0; d<rows; d++) {
    //grid[c][d].canHighlight();
    grid[c][d].update();
    }
    }
    }
    else {
    locked = false;
    }
    }

    class CircleButton
    {

    color basecolor;
    int x, y;
    int size;
    color highlightcolor;
    color currentcolor;
    boolean highlight;
    boolean over = false;
    boolean pressed = false;
    CircleButton(int ix, int iy, int isize, color icolor, color ihighlight)
    {
    x = ix;
    y = iy;
    size = isize;
    highlightcolor = ihighlight;
    currentcolor = icolor;
    highlight = true;
    }
    void setHighlight() {
    highlight = false;
    }

    void update()
    {
    if (over() && highlight == true ) {
    currentcolor = highlightcolor;
    }
    else if (currentcolor == highlightcolor && !over()) {
    currentcolor = color(255);
    }
    else {
    currentcolor = currentcolor;
    }
    }

    boolean pressed()
    {
    if (over) {
    locked = true;
    return true;
    }
    else {
    locked = false;
    return false;
    }
    }


    boolean overCircle(int x, int y, int diameter)
    {
    float disX = x - mouseX;
    float disY = y - mouseY;
    if (sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
    return true;
    }
    else {
    return false;
    }
    }



    void setColor(color a) {
    currentcolor = a;
    }
    color getColor() {
    return currentcolor;
    }

    boolean over()
    {
    if ( overCircle(x, y, size) ) {
    over = true;
    return true;
    }
    else {
    over = false;
    return false;
    }
    }

    void display()
    {
    smooth();
    stroke(currentcolor);
    fill(currentcolor);
    ellipse(x, y, size, size);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Connect Four Game need help pleaseee!!

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    The posted code is missing some parts and will not compile to allow execution for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connect Four Game need help pleaseee!!

     
    int count = 0;
    int rows = 6;
    int cols = 7;
    int winner = 0;
    CircleButton[][] grid = new CircleButton[cols][rows];
    int[][] gridChecker = new int[cols][rows];
    boolean locked = false;
    boolean gameOn = true;
     
    void setup()
    {
    smooth();
    for (int a = 0; a<cols; a++) {
    for (int b = 0; b<rows; b++) {
    gridChecker[a][b] = 10;
    }
    }
    int a;
    int b;
    for ( a = 0; a<cols; a++) {
    for (b = 0; b<rows; b++) {
    grid[a][b] = new CircleButton(100*a + 50, 100*b + 50, 80, color(255), color(153));
    }
    }
     
    color buttoncolor = color(204);
    color highlight = color(153);
    ellipseMode(CENTER);
     
    size(700, 600);
    background(255, 204, 0);
     
     
    stroke(126);
    for ( int c = 1; c <= cols; c++) {
    line( c*(width/cols), 0, c*(width/cols), height);
    }
    for (int c = 1; c<=rows; c++) {
    line(0, c*(height/rows), width, c*(height/rows));
    }
    }
    void draw()
    {
    update(mouseX, mouseY);
    for (int c = 0; c<cols; c++) {
    for (int d = 0; d<rows; d++) {
    grid[c][d].display();
    }
    }
    if (checkWin() == true) {
    gameOn = false;
    PFont font = loadFont("IskoolaPota-48.vlw");
    textFont(font, 80);
    if (winner == 4) {
    shadowtext("Red Wins!", width/4, height/2, 3);
    }
    else if(winner == 8) {
    shadowtext("Black Wins!", width/3.5, height/2, 3);
    }
    shadowtext("Click to play again", width/5.5, height*.3, 1);
    }
     
    }
    void shadowtext (String s, float x, float y, int o) {
    fill(100,100);
    text(s, x+o, y+o);
    fill(58,12,247);
    text(s, x, y);
    }
    void mousePressed() {
    if(gameOn == false) {
    gameOn = true;
    setup();
    }
    }
     
    boolean checkWin()
    {
     
    int counter;
    //horizontal
    for (int a=0; a < rows; a++) {
    for (int b=0; b < cols-3; b++) {
    int tCheck = (gridChecker[b][a]) + (gridChecker[b+1][a]) + (gridChecker[b+2][a]) + (gridChecker[b+3][a]);
    if (tCheck == 8 || tCheck == 4)
    {
    winner = tCheck;
    return true;
    }
    }
    }
     
     
    //vertical
    for (int a=0; a < rows-3; a++) {
    for (int b=0; b < cols; b++) {
    int tCheck = (gridChecker[b][a]) + (gridChecker[b][a+1]) + (gridChecker[b][a+2]) + (gridChecker[b][a+3]);
    if (tCheck == 8 || tCheck == 4)
    {
    winner = tCheck;
    return true;
    }
    }
    }
     
     
    //diagonals
    for (int a=0; a < rows-3; a++) {
    for (int b=0; b < cols-3; b++) {
    int tCheck = (gridChecker[b][a]) + (gridChecker[b+1][a+1]) + (gridChecker[b+2][a+2]) + (gridChecker[b+3][a+3]);
    if (tCheck == 8 || tCheck == 4)
    {
    winner = tCheck;
    return true;
    }
    }
    }
     
     
    for (int a =0; a < rows-3; a++) {
    for (int b=0; b < cols-3; b++) {
    int tCheck = (gridChecker[b][a]) + (gridChecker[b+1][a+1]) + (gridChecker[b+2][a+2]) + (gridChecker[b+3][a+3]);
    if (tCheck == 8 || tCheck == 4)
    {
    winner = tCheck;
    return true;
    }
    }
    }
     
    for (int a=0; a < rows-3; a++) {
    for (int b=3; b < cols; b++) {
    int tCheck = (gridChecker[b][a]) + (gridChecker[b-1][a+1]) + (gridChecker[b-2][a+2]) + (gridChecker[b-3][a+3]);
    if (tCheck == 8 || tCheck == 4)
    {
    winner = tCheck;
    return true;
    }
    }
    }
     
    return false;
    }
     
     
     
    void update(int x, int y)
    {
     
    if (mousePressed && gameOn) {
    for (int c = 0; c<cols; c++) {
    for (int d = 0; d<rows; d++) {
    if (grid[c][d].pressed() && (grid[c][d].getColor() == color(255)|| grid[c][d].getColor() == color(153)) ) {
    if (d == 5 || (gridChecker[c][d + 1] == 1 || gridChecker[c][d+1] == 2)) {
    grid[c][d].setHighlight();
    count++;
    if (count % 2 == 0) {
    grid[c][d].setColor(color(255, 0, 0));
    gridChecker[c][d] = 1;
    }
    else {
    grid[c][d].setColor(color(0));
    gridChecker[c][d] = 2;
    }
    }
    }
    }
    }
    }
    if (locked == false && gameOn) {
    for (int c = 0; c<cols; c++) {
    for (int d = 0; d<rows; d++) {
    //grid[c][d].canHighlight();
    grid[c][d].update();
    }
    }
    }
    else {
    locked = false;
    }
    }
     
    class CircleButton
    {
     
    color basecolor;
    int x, y;
    int size;
    color highlightcolor;
    color currentcolor;
    boolean highlight;
    boolean over = false;
    boolean pressed = false; 
    CircleButton(int ix, int iy, int isize, color icolor, color ihighlight)
    {
    x = ix;
    y = iy;
    size = isize;
    highlightcolor = ihighlight;
    currentcolor = icolor;
    highlight = true;
    }
    void setHighlight() {
    highlight = false;
    }
     
    void update()
    {
    if (over() && highlight == true ) {
    currentcolor = highlightcolor;
    }
    else if (currentcolor == highlightcolor && !over()) {
    currentcolor = color(255);
    }
    else {
    currentcolor = currentcolor;
    }
    }
     
    boolean pressed()
    {
    if (over) {
    locked = true;
    return true;
    }
    else {
    locked = false;
    return false;
    }
    }
     
     
    boolean overCircle(int x, int y, int diameter)
    {
    float disX = x - mouseX;
    float disY = y - mouseY;
    if (sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
    return true;
    }
    else {
    return false;
    }
    }
     
     
     
    void setColor(color a) {
    currentcolor = a;
    }
    color getColor() {
    return currentcolor;
    }
     
    boolean over()
    {
    if ( overCircle(x, y, size) ) {
    over = true;
    return true;
    }
    else {
    over = false;
    return false;
    }
    }
     
    void display()
    {
    smooth();
    stroke(currentcolor);
    fill(currentcolor);
    ellipse(x, y, size, size);
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Connect Four Game need help pleaseee!!

    The posted code is missing some parts and will not compile to allow execution for testing.

    Also the posted code is not properly formatted. All statements should not start in the first column. Nested statements should be indented.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connect Four Game need help pleaseee!!

    The program that I am using is called Processing which is a little different then your standard java coding with eclipse.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Connect Four Game need help pleaseee!!

    Without fully defined classes that compile without errors, its not possible to test the program.

    BTW I don't use eclipse.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connect Four Game need help pleaseee!!

    The code is all one class, which makes up the game which is fully functional, I just wanted help with putting movement in and rather then clicking on the board and having the chip appear I wanted them to have motion and have them drop to the bottom of the board.

    --- Update ---

    Connect Four- OpenProcessing

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Connect Four Game need help pleaseee!!

    If you can't make and post a complete program that compiles, executes and shows the problem, there is no way to test the program and see what it does.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with Snake Game Java Code: It's Impossible to Lose the Game
    By haruspex_icis in forum What's Wrong With My Code?
    Replies: 20
    Last Post: December 17th, 2012, 12:21 PM
  2. help pleaseee
    By katarina in forum Other Programming Languages
    Replies: 3
    Last Post: May 6th, 2012, 03:41 PM
  3. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM
  4. CONNECT FOUR GAME-MINIMAX TO ALPHABETA ALGORITHM
    By AJreal in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 6th, 2011, 03:30 PM
  5. How to connect keyboard through COM?
    By yogesh01 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 6th, 2010, 05:00 AM