Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Java program for connectfour game
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Sumoon
Java program for connectfour game
0 Answer(s)      a year and 4 months ago
Posted in : Java Beginners


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

Advertisement
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
java game
java game  Can anyone please give me a game source code
Advertisements
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
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
game Nim - Java Beginners
game Nim  how is the search tree for a game Nim looks like
game
game  How to make 3D game in java Applet
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  how can create simple game in java . thank u so match ,please help
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
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
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 poramboku.me@gmai.com
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
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
tetris game code - Swing AWT
tetris game code  To develop a JAVA puzzle game which is a "variation" of the Tetris game
game programming
game programming  Hi. i have witten the script and have almost completed developing a social game like mafia wars. can any one plz help me by answering me some of my doubts.. after uploading the game on facebook how tokeep data
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
Sudoku program - Java Beginners
Sudoku program  I would like to get the program for 9X9 Sudoku game in JAVA
Blackjack game - IDE Questions
Blackjack game  Create a java code for a simple blackjack game for just one hand of Blackjack, it does not need to include money and betting
Chess Game
Chess Game  How could I make a basic 8x8 chess table with two dimensional arrays and insert the chess pieces in the array?   Please visit the following link: http://roseindia.net/java/example/java/swing/chess
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 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
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
game programming code - Swing AWT
game programming code  write a snake program using swings with step by step explanation?. please send me this source code to my mail id with step by step explanation
Two user chess game - Applet
Two user chess game  Write a program for a two user chess game(Users must be on different systems)? please send me this source code to my mail id with step by step explanation
java program for
java program for   java program for printing documents,images and cards
Can anyone assist with the coding of this game?
Can anyone assist with the coding of this game?  I am in need of the coding for this game. I need to write a "Hangman" game that will randomly... appreciate all effort for this program
Create a guessing game - Framework
Create a guessing game  Create a guessing game that picks a random number between 1 and 100 and lets you keep guessing until you get it right! You..., too. it is in java but i m not sure about the language, it is like c.print
Advertisements
 

 

 

DMCA.com