Java program for connectfour game

Java program for connectfour game

Hello, my program is about connectFour game. I need help with resolving the logic for this problem. I appreciate your help very much.

**Client:**

package FourPack;

import javax.swing.*;

/* Algorithm
    Level 0:
        Continue until the user wishes to quit
            Prompt the user to select whether they will be playing with another player or against the computer
            Prompt the user to select whether they will be player 1(yellow) or player 2(red)
            Player 1  selects which column they will be placing their token in
            Place the token at the top of the chosen column
            Repeat the same process for player 2 
            If either player attempts to place the token in a full column, output an error message
            Inform each player of how many tokens each has remaining
            If either player gets four of their tokens in consecutive diagonal, horizontal, or vertical order before the other, they win
*/
public class ConnectFourClient extends GBFrame 
{
    private JButton quit, player1, player2, newGame, col1, col2, col3, col4, col5, col6, col7;
    private JTextArea board;
    private JLabel title;
    private int playerNum;
    ConnectFourServer game = new ConnectFourServer();
    private ConnectFourClient()
    {
        title  = addLabel("Connect Four", 1,4,1,1);
        player1 = addButton("Player 1: Yellow", 2,6,1,1);
        player2 = addButton("Player 2: Red", 2,7,1,1);
        col1 = addButton("Add Token", 3,1,1,1);
        col2 = addButton("Add Token", 3,2,1,1);
        col3 = addButton("Add Token", 3,3,1,1);
        col4 = addButton("Add Token", 3,4,1,1);
        col5 = addButton("Add Token", 3,5,1,1);
        col6 = addButton("Add Token", 3,6,1,1);
        col7 = addButton("Add Token", 3,7,1,1);
        quit = addButton("Quit", 12,7,1,1);
        board = addTextArea("", 4,1,7,6);
        playerNum = 0;
    }
    public void buttonClicked(JButton b)
    {
        if(b==player1)
        {
            playerNum = game.selectPlayer(1);
            messageBox("You are player " + playerNum + ".");
        }
        else if(b==player2)
        {
            playerNum = game.selectPlayer(2);
            messageBox("You are player " + playerNum + ".");
        }
        else if(b==col1)
        {
            if(playerNum==1)
            {
                game.play(0,'y');
                board.setText(game.getBoard());
                if(game.turn()==2)
                {
                    messageBox("It is player2's turn.");
                }
            }
            else if(playerNum==2)
            {
                game.play(0,'r');
                board.setText(game.getBoard());
            }
        }
        else if(b==col2)
        {
            if(playerNum==1)
            {
                game.play(1,'y');
                board.setText(game.getBoard());
            }
            else if(playerNum==2)
            {
                game.play(1,'r');
                board.setText(game.getBoard());
            }
        }
        else if(b==col3)
        {
            if(playerNum==1)
            {
                game.play(2,'y');
                board.setText(game.getBoard());
            }
            else if(playerNum==2)
            {
                game.play(2,'r');
                board.setText(game.getBoard());
            }
        }
        else if(b==col4)
        {
            if(playerNum==1)
            {
                game.play(3,'y');
                board.setText(game.getBoard());
            }
            else if(playerNum==2)
            {
                game.play(3,'r');
                board.setText(game.getBoard());
            }
        }
        else if(b==col5)
        {
            if(playerNum==1)
            {
                game.play(4,'y');
                board.setText(game.getBoard());
            }
            else if(playerNum==2)
            {
                game.play(4,'r');
                board.setText(game.getBoard());
            }
        }
        else if(b==col6)
        {
            if(playerNum==1)
            {
                game.play(5,'y');
                board.setText(game.getBoard());
            }
            else if(playerNum==2)
            {
                game.play(5,'r');
                board.setText(game.getBoard());
            }
        }
        else if(b==col7)
        {
            if(playerNum==1)
            {
                game.play(6,'y');
                board.setText(game.getBoard());
            }
            else if(playerNum==2)
            {
                game.play(6,'r');
                board.setText(game.getBoard());
            }
        }
        else if(b==quit)
        {
            System.exit(0);
        }
    }
    public static void main(String[] args)
    {
        ConnectFourClient theGUI = new ConnectFourClient();
        theGUI.setSize(800, 600);
        theGUI.setVisible(true);
        theGUI.setTitle("Connect Four");
    }
}


**Server:**


package FourPack;
import BreezySwing.*;
public class ConnectFourServer 
{
    char[][] gameBoard = new char[6][7];
    int yellowLeft, redLeft, player, turn, num, full;
    boolean filled;
    /* The game board is a grid of 6 rows and 7 columns.
     * The character 'y' represents the placement of player one's token(yellow)
     * The character 'r' represents the placement of player two's token(red)
     * If a position in the array is set to null, it is indicated that a token has
     * not been placed in that spot yet.
     * Rows are numbered 1-6, and columns are 1-7.
       The index of a position in the array is its column# -1 and row# -1.*/
    public ConnectFourServer()
    {
        yellowLeft = 21;
        redLeft = 21;
        turn = 0;
        num = 2;
        full = 0;
        filled = false;
    }
    public int selectPlayer(int c)
    {
        if(c==1)
        {
            player = 1;
        }
        else if(c==2)
        {
            player = 2;
        }
        return player;
    }
    public char[][] play(int p, char color)
    {
        for(int i = 5; i>=0; i-- )
        {
            if(gameBoard[i][p]=='\u0000')
            {
                gameBoard[i][p] = color;
                break;
            }
        }
        full++;
        return gameBoard;
    }
    public String getBoard()
    {
        String sb = "";
        for(int i = gameBoard.length-1; i>=0; i--)
        {
            for(int j = 0; j < gameBoard[i].length-1; j++)
            {
                sb = sb +  Format.justify('c', gameBoard[i][j], 15) + "|";
            }
            sb += "\n-\n";
        }
        return sb;
    }
    public String checkBoard()
    {
        String check = "";
        if(filled==true)
        {
            check = "rf";
        }
        if(full==42)
        {
            check = check + "bf";
        }
        return check;
    }
    public boolean getWin(int c)
    {
        boolean win = false;
        for(int i = 5; i>=2; i--)
        {

        }
        return win;
    }
    public int turn()
    {
        if(num%2!=0)
        {
            turn = 1;
        }
        else
            turn = 2;
        num++;
        return turn;
    }
}
View Answers









Related Tutorials/Questions & Answers:
Java program for connectfour game
Java program for connectfour game  Hello, my program is about connectFour game. I need help with resolving the logic for this problem. I appreciate...; ConnectFourServer game = new ConnectFourServer(); private ConnectFourClient
ModuleNotFoundError: No module named 'botany-connectfour'
ModuleNotFoundError: No module named 'botany-connectfour'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'botany-connectfour' How to remove the ModuleNotFoundError: No module
Advertisements
ModuleNotFoundError: No module named 'botany-connectfour'
ModuleNotFoundError: No module named 'botany-connectfour'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'botany-connectfour' How to remove the ModuleNotFoundError: No module
java game
java game  Can anyone please give me a game source code
JAVA Game
JAVA Game  I want to make a JAVA game...in dat game...d player has to input a word in 10 secnds...4 dat m running a delay loop... but the problem is not...i am not getting any way...to make that input field(eg: String str
Game
Game  How to develop a game using java? give me a total code for any simple game
game
game  How to make 3D game in java Applet
Game
Game  how can create simple game in java . thank u so match ,please help
game Nim - Java Beginners
game Nim  how is the search tree for a game Nim looks like
Create a Swamp Game in Java
Create a Swamp Game in Java  You are to create a game called â??Get Out of My Swampâ??. In this game an ogre, called â??Hekâ??, wanders around his... two ogre enemies in the same place they kill the ogre and the game ends. Full
Java Game Pontoon
Java Game Pontoon  Could someone help me create a programme for the game pontoon //play a game of pontoon import java.util.*; public class Pontoon { public static void main (String args[]) { //deal initial hands
game
game  Hi thank you for answers.I am devoloping a java game caled Duck Hunt Game in netbeans6.7.1 software and I need help in some codes...,if you have this game's code please post me as soon as possible
Java game Pontoon
Java game Pontoon  Hello. I have trouble with my pontoon code. I ahve done it using simple java programming. It is working BUT it dosent stops when the players hand rises ober 24 points. where in game the player should be bust
Java Program
Java Program  You are to create a game called â??Get Out of My Swampâ??. In this game an ogre, called â??Hekâ??, wanders around his swamp, if he... enemies in the same place they kill the ogre and the game ends. Full details
Java Program
Java Program  You are to create a game called â??Get Out of My Swampâ??. In this game an ogre, called â??Hekâ??, wanders around his swamp, if he... enemies in the same place they kill the ogre and the game ends. Full details
Craps Game Java Programming Help
Craps Game Java Programming Help  Craps is a casino game... solution in a file called Craps.java We wish to write a program that simulates... will be ignored by the grading program. Your result should be outputted using
who know full coding of guessing game in java?
who know full coding of guessing game in java?  There is only 1 player. Use JAVA Random toolkit to generate random number between 1 to 10. Once user... a number. The program should display user win if the number user has guess
Sudoku program - Java Beginners
Sudoku program  I would like to get the program for 9X9 Sudoku game in JAVA
BRICK-BREAKER game concept - Java Beginners
BRICK-BREAKER game concept  I am planning to write a program for BRICK-BREAKER GAME. can any 1 help me any 1 can u plz help me frzzz?? you can also send the mails to [email protected]
java program
java program  hello.. do u all have any source to do the java programming for Snake and Ladder Game or any reservation system.. help me plz
Java game bulls and cows
Java game bulls and cows In this tutorial, you will learn how to implement bulls and cows game in java. The game bulls and cows is an ancient game...;quit", the user will come out from the game. If the user inputs 4 digit
Java Program
Java Program  Problem Statement You are required to play a word-game with a computer. The game proceeds as follows The computer 'thinks' of a four..., to make the game a bit challenging, the computer does not tell us which letter
Java Program - Swing AWT
Java Program  A program to create a simple game using swings.  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/java/swing/guessNumber.html Thanks
genaral snake game in java - Java Beginners
genaral snake game in java  can u please send code for fallowing write a snake game program using swings?  Hi This is the source code of Snake game in swing Snake.java file import javax.swing.JFrame
Java Program
Java Program  Problem Statement A game is made around the basic concept of a man pushing a box in a maze, from its source to its destination. The maze consists of cells that are empty or have obstacles. The box is heavy
java program for
java program for   java program for printing documents,images and cards
a Java program
a Java program    Write a Java program to print even numbers from 2 to 1024? Write a Java program to print ? My Name is Mirza? 100 times? Write a Java program to print Fibonacci Series? Write a Java program to reverse a number
Java Program
Java Program  A Java Program that print the data on the printer but buttons not to be printed
java program
java program  write a program to print 1234 567 89 10
java program
java program  Write a program to demonstrate the concept of various possible exceptions arising in a Java Program and the ways to handle them.  ... in Java
java program
java program  write java program for constructor,overriding,overriding,exception handling
java program
java program  how to write an addition program in java without using arithematic operator
java program
java program  write a java program to display array list and calculate the average of given array
java program
java program  write a java program to display array list and calculate the average of given array
Java Program
Java Program  java program to insert row in excel sheet after identifying an object
java program
java program  java program to implement the reflection of a particular class details like constructor,methods and fields with its modifiers
java program
java program  Write a java program to do matrix addition operation On two given matrices
java program
java program  Write a java program to find the number of Positive numbers in m* n matrix
java program
java program  Write a program to create an applet and display The message "welcome to java
java program
java program  hi friends how to make a java program for getting non prime odd numbers in a given series
java program
java program   Write a program to find the difference between sum of the squares and the square of the sums of n numbers
java program
java program  write a program to create text area and display the various mouse handling events
java program
java program  Develop the program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The program accpets three values: the pipes inner radius, its length, and the thickness of its wall
java program
java program  . Develop the program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The program accpets three values: the pipes inner radius, its length, and the thickness of its wall
java program
java program  . Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any type of numeric values and returns the results
Java program
Java program  Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any type of numeric values and returns the results
program in java
program in java  write a reverse program in java using string buffer.the input and out put as follows. input- hi good mornig out put-ih doog ginrom
java program
java program  write a java program to compute area of a circle.square,rectangle.triangle,volume of a sphere ,cylinder and perimeter of cube using method over riding
java program
java program  write a java program to compute area of a circle.square,rectangle.triangle,volume of a sphere ,cylinder and perimeter of cube using method over riding
java program
java program   A B C D E F F E D C B A A B C D E E D C B A A B C D D C B A A B C C B A A B B A A A java program to display above triangle

Ads