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 6 of 6

Thread: Need a win aspect in connect four game

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

    Default Need a win aspect in connect four game

    I want to have the game stop when you connect four and have the screen say what color one and the click to reset. This is what I have so far.

    <color yellow = color (255, 217, 0);
    color red = color (255, 0, 0);
    color black = color (0, 0, 0);
    int winner = 0;
    int rows = 6;
    int cols = 7;
     
    int[][] gridChecker = new int[cols][rows];
    boolean gameOn = true;
    boolean locked = false;
     
    boolean isRed = true;
     
     
    void draw () {
      smooth();
     
      background (yellow);
      for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 6; j++) {
          a[i][j].render();
          a[i][j].mouseOver();
        }
      }
      stroke (20);
      line (100, 0, 100, 600);
      line (200, 0, 200, 600);
      line (300, 0, 300, 600);
      line (400, 0, 400, 600);
      line (500, 0, 500, 600);
      line (600, 0, 600, 600);
      line (0, 100, 700, 100);
      line (0, 200, 700, 200);
      line (0, 300, 700, 300);
      line (0, 400, 700, 400);
      line (0, 500, 700, 500);
    }
     
     {
      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 i=0; i < rows; i++) {
          for (int j=0; j < cols-3; j++) {
            int tCheck = (gridChecker[j][i]) + (gridChecker[j+1][i]) + (gridChecker[j+2][i]) + 
              (gridChecker[j+3][i]);
            if (tCheck == 8 || tCheck == 4)
            {
              winner = tCheck;
              return true;
            }
          }
        }
     
     
        //vertical
        for (int i=0; i < rows-3; i++) {
          for (int j=0; j < cols; j++) {
            int tCheck = (gridChecker[j][i]) + (gridChecker[j][i+1]) + (gridChecker[j][i+2]) + 
              (gridChecker[j][i+3]);
            if (tCheck == 8 || tCheck == 4)
            {
              winner = tCheck;
              return true;
            }
          }
        }
     
     
        //diagonals
        for (int i=0; i < rows-3; i++) {
          for (int j=0; j < cols-3; j++) {
            int tCheck = (gridChecker[j][i]) + (gridChecker[j+1][i+1]) + (gridChecker[j+2][i+2]) + 
              (gridChecker[j+3][i+3]);
            if (tCheck == 8 || tCheck == 4)
            {
              winner = tCheck;
              return true;
            }
          }
        }
     
     
        for (int i =0; i < rows-3; i++) {
          for (int j=0; j < cols-3; j++) {
            int tCheck = (gridChecker[j][i]) + (gridChecker[j+1][i+1]) + (gridChecker[j+2][i+2]) + 
              (gridChecker[j+3][i+3]);
            if (tCheck == 8 || tCheck == 4)
            {
              winner = tCheck;
              return true;
            }
          }
        }
     
        for (int i=0; i < rows-3; i++) {
          for (int j=3; j < cols; j++) {
            int tCheck = (gridChecker[j][i]) + (gridChecker[j-1][i+1]) + (gridChecker[j-2][i+2]) + 
            (gridChecker[j-3][i+3]);
            if (tCheck == 8 || tCheck == 4)
            {
              winner = tCheck;
              return true;
            }
          }
        }
     
        return false;
      }
     
     
     
    void mouseClicked() {
      for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 6; j++) {
          if (a[i][j].isFilled == false) {
            if (mouseX > a[i][j].x - a[i][j].diameter / 2 &&
              mouseX < a[i][j].x + a[i][j].diameter / 2 &&
              mouseY > a[i][j].y - a[i][j].diameter / 2 &&
              mouseY < a[i][j].y + a[i][j].diameter / 2)
            {
     
              a[i][j].fillColor = (isRed ? red : black);
              a[i][j].isFilled = true;
              gridChecker[i][j] = (isRed ? red : black);
     
              isRed = !isRed;
            }
          }
        }
      }
    }
    >


    <class Circle {
      float x;
      float y;
     
      float diameter;
      boolean isFilled; 
     
      color fillColor;
      color strokeColor;
     
      Circle(float init_x, float init_y,
      float init_diameter,
      color init_color)
      {
        x = init_x;
        y = init_y;
        diameter = init_diameter;
        fillColor = init_color;
        strokeColor = init_color;
        isFilled = false;
      }
     
      void render () {
        fill (fillColor);
        stroke (strokeColor);
        ellipse (x, y, diameter, diameter);
      }
     
      void mouseOver() {                          // determines which cirlce the mouse is over, can help with making next move
        if (isFilled == false) {
          if (mouseX > x - diameter / 2 &&
            mouseX < x + diameter / 2 &&
            mouseY > y - diameter / 2 &&
            mouseY < y + diameter / 2)
          {
            fillColor = (isRed ? red : black);
          }
          else {
            fillColor = (255);
          }
        }
      }
    } // class Ball
     
    Circle [][] a = new Circle[7][6];
     
    void setup() {
      size(700, 600);
      smooth();
      for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 6; ++j) {
          a[i][j] = new Circle (50 + 100*(i), 50 + 100*(j), 90, color (255, 255, 255));
          gridChecker[i][j] = 10;
        }
      }
    }
    >


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Need a win aspect in connect four game

    Can you explain what the code does and what your problems are?

    See: Connect Four Game need help pleaseee!!
    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: Need a win aspect in connect four game

    The code sets up a connect four board with a mouse listener that when clicked highlights the empty spaces red or black you have to win four chips in a row vertically horizontally or diagonally. I use a grid checker to see if you have these wins. But it does not work or check it properly

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Need a win aspect in connect four game

    How can the code be compiled and executed for testing?
    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: Need a win aspect in connect four game

    {
    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);
    }



    boolean checkWin()
    {

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


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


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


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

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

    return false;
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Need a win aspect in connect four game

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

    If the code can't be compiled and executed for testing, don't bother posting it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Connect Four Game need help pleaseee!!
    By newmanj8 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 26th, 2013, 12:57 PM
  2. [SOLVED] Working on Win 7 and not on XP
    By shaumux in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 4th, 2011, 06:36 PM
  3. CONNECT FOUR GAME-MINIMAX TO ALPHABETA ALGORITHM
    By AJreal in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 6th, 2011, 03:30 PM