Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training Big Data and Hadoop Training Java Training  
 

Java program for connectfour game

Ads

 
 

Share on Google+Share on Google+

Sumoon
Java program for connectfour game
0 Answer(s)      4 years and 2 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;
    }
}
Ads
View Answers
Ads









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
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 develop a game using java? give me a total code for any simple game
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 to make 3D game in java Applet
Game
Game  how can create simple game in java . thank u so match ,please help
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
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
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 [email protected]
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  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
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  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 for
java program for   java program for printing documents,images and cards
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
Java Program  A Java Program that print the data on the printer but buttons not to be printed
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  write a program to print 1234 567 89 10
java program
java program  how to write an addition program in java without using arithematic operator
java program
java program  write java program for constructor,overriding,overriding,exception handling
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