Question

IN JAVA - GAME IS CHECKERS. I AM IN COMP SCIENCE 1 PLEASE DO NOT USE...

IN JAVA - GAME IS CHECKERS. I AM IN COMP SCIENCE 1 PLEASE DO NOT USE TOO ADVANCED CODE OR ELSE IT IS WORTHLESS TO ME. IT IS A SIMPLE TEXT BOARD GAME.

The project task is to develop a program where a human can play a computer opponent at a simple game of checkers, where the game pieces are represented by 'O' for red and 'X' for black. You are responsible for designing the representation of the board, developing prompts for the user to make moves, testing whether those moves are legal according to the rules, generating the moves of the computer opponent, and determining when someone wins by hopping the other pieces diagonally.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here i had given the code for gui checkers gamein java. please give me a like. it helps me a lot.

/*

File: GameWindow.java

Abstract: Parent frame containing all other GUI components

*/

import java.io.*;

import java.lang.*;

import java.util.Vector;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

public class GameWindow extends JFrame {

Checkers control;

Square[][] squares;

Board board;

Vector movelist;

GameSelector selector;

int turnNumber;

Container top;

JPanel border;

JPanel buttons;

JButton done;

JButton clear;

JButton backstep;

JPanel outborder;

JTextField output;

JMenuBar menubar;

JMenuItem newgame;

JMenuItem quit;

public GameWindow(Checkers chk){

    this();

    control = chk;

    selector = new GameSelector();

}

public GameWindow(){

    //default constructor for testing GUI

    super("Checkers");

    turnNumber = 0;

    top = getContentPane();

   

    initiateSquares();

    createBoard();

    createButtons();

    createOutput();

    makeMenuBar();

    addListeners();

    top.add(menubar, BorderLayout.NORTH);

    top.add(outborder, BorderLayout.SOUTH);

    top.add(border, BorderLayout.WEST);

    top.add(buttons, BorderLayout.EAST);

    setResizable(false);

    pack();

    setVisible(true);

}

private void initiateSquares(){

    //create the listing for the individual squares and pieces

    squares = new Square[8][8];

    int count = 1;

    for (int r = 0; r < 8; r++){

      for (int c = 0; c < 8; c++){

        if (((r + c) % 2) != 1){

          squares[c][r] = new Square(Color.white, -1);

        }

        else {

          squares[c][r] = new Square(Color.blue, count);

          count++;

          if (r < 3){

            squares[c][r].arrive(new Piece(Color.black));

          }

          if (r > 4){

            squares[c][r].arrive(new Piece(Color.red));

          }

        }

      }

    }

}

private void createBoard(){

    border = new JPanel();

    border.setBorder(new EmptyBorder(20, 20, 20, 20));

    board = new Board(this);

    border.add(board);

}

private void createButtons(){

    buttons = new JPanel();

    buttons.setLayout(new GridLayout(10,1));

    done = new JButton("Done");

    clear = new JButton("Clear");

    backstep = new JButton("Backstep");

    buttons.setBorder(new EmptyBorder(25,0,20,20));

    done.setSize(backstep.getSize());

    clear.setSize(backstep.getSize());

    backstep.setEnabled(false);

    buttons.add(done);

    buttons.add(clear);

    buttons.add(backstep);

}

private void createOutput(){

    outborder = new JPanel();

    outborder.setBorder(new EmptyBorder(0,20,20,20));

    output = new JTextField(45);

    outborder.add(output);

}

private void makeMenuBar(){

    menubar = new JMenuBar();

    newgame = new JMenuItem("New_Game");

    quit = new JMenuItem("Quit");

    menubar.add(newgame); menubar.add(quit);

    for (int i = 0; i < 4; i++){

      menubar.add(new JMenuItem());

    }

}

private void addListeners(){

    addWindowListener(new WindowAdapter() {

        public void windowClosing(WindowEvent e) {

          if (control == null) System.exit(0);

          control.send("exit");

        }

      });

    quit.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent evt){

          if (control == null) System.exit(0);

          control.send("exit");

        }

      });

   

    newgame.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent evt){

          initiateSquares();

          board.repaint();

        turnNumber = 0;

          nextTurn();

          if (control != null){

            control.send("newgame");

          }

        }

      });

    done.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent evt){

          movelist = board.getMove();

          if (!movelist.isEmpty()){

            Point p = null;

            String move = " ";

            for (int i = 0; i < movelist.size(); i++){

              p = (Point)(movelist.elementAt(i));

              move = move + squares[p.x][p.y].getNumber() + " ";

            }

            board.clearMove();

            if (control != null) control.send(move);

          }

        }

      });

    clear.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent evt){

          board.clearMove();

        }

      });

    backstep.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent evt){

          board.backup();

        }

      });

}

//---------------------------------------------------------------

//----------------communication methods--------------------------

public Square[][] getSquares(){

    return squares;

}

public void enableBackstep(boolean b){

    backstep.setEnabled(b);

}

public void display(String txt){

    output.setText(txt);

}

public void setSquare(int i, Piece p){

    Square s = null;

    for (int r = 0; r < 8; r++){

      for (int c = 0; c < 8; c++){

        s = squares[r][c];

        if (s.getNumber() == i) s.arrive(p);

      }

    }

}

public void nextTurn(){

    if (turnNumber%2 == 0)

      display("Turn " + turnNumber + ": Dark Player's Turn ...");

    else { display("Turn " + turnNumber + ": Light Player's Turn ..."); }

    turnNumber++;

}

public void newGame(){

    selector.setVisible(true);

    String gameselection = selector.newGameSelection();

    selector.setVisible(false);

    System.out.println(gameselection);

    control.send(gameselection);

}

public void endGame(String winner){

    Graphics g = board.getGraphics();

    int color = winner.indexOf(" ");

    int size = board.getBoardSize();

    String win = winner.substring(0,color).trim();

    String mess = winner.substring(color).trim();

    if (win.equals("dark")){

      g.setColor(Color.red);

     g.fillRect(0,0,size,size);

      g.setColor(Color.black);

    }

    else {

      g.setColor(Color.black);

      g.fillRect(0,0,size,size);

      g.setColor(Color.red);

    }

    g.drawString(mess, 75, size/2);

    display("Click New_Game to play again");

}

public static void main(String args[]){

    GameWindow gw = new GameWindow();

}

}
/*

File: Board.java

Abstract: Panel for drawing the representation of the checker "world"

*/

import java.io.*;

import java.lang.*;

import java.util.Vector;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

class Board extends JPanel {

GameWindow parent;

Vector movelist;

int size = 401;

int sqsz = (size-1)/8;

public Board(GameWindow gw){

    parent = gw;

    movelist = new Vector();

    setPreferredSize(new Dimension(size,size));

    addMouseListener( new MouseListener(){

        public void mouseEntered(MouseEvent evt){}

        public void mouseExited(MouseEvent evt){}

        public void mousePressed(MouseEvent evt){}

        public void mouseReleased(MouseEvent evt){}

        public void mouseClicked(MouseEvent evt){

          int x = (int)(evt.getX()/sqsz);

          int y = (int)(evt.getY()/sqsz);

          Point p = new Point(x,y);

          addPoint(p);

          highlightSquare(x,y);

        }

      });

}

private void highlightSquare(int x, int y){

    Graphics g = this.getGraphics();

    g.setColor(Color.red);

    g.drawRect((x*sqsz), (y*sqsz), sqsz, sqsz);

}

private void addPoint(Point p){

    if (movelist.size() == 0){

      movelist.add(p);

      parent.enableBackstep(true);

    }

    else {

      boolean found = false;

      for(int i = 0; i < movelist.size(); i++){

        found = ((Point)movelist.elementAt(i)).equals(p);

      }

      if (!found){

        movelist.add(p);

        parent.enableBackstep(true);

      }}}

public Vector getMove(){

    return movelist;}

public int getBoardSize(){

    return size-1;}

public void clearMove(){

    movelist.removeAllElements();

    parent.enableBackstep(false);

    repaint();}

public void backup(){

    if (movelist.size() > 0){

      int x = ((Point)(movelist.elementAt(movelist.size() - 1))).x;

      int y = ((Point)(movelist.elementAt(movelist.size() - 1))).y;

      movelist.removeElementAt(movelist.size() - 1);

      movelist.trimToSize();

      if (movelist.size() <= 0) parent.enableBackstep(false);

      Graphics g = this.getGraphics();

      g.setColor(Color.black);

      g.drawRect((x*sqsz), (y*sqsz), sqsz, sqsz);}}

   

public void paint(Graphics g){

    Square[][] grid = parent.getSquares();

    for (int r = 0; r < 8; r++){

      for (int c = 0; c < 8; c++){

        g.setColor(grid[r][c].getColor());

        g.fillRect((r*sqsz), (c*sqsz), sqsz, sqsz);

        Piece p = grid[r][c].occupant();

        if (p != null){

          g.setColor(p.getColor());

          g.fillOval((r*sqsz+10), (c*sqsz+10), 30, 30);

          if (p.isKing()){

            //draw king representation

            g.setColor(Color.yellow);

            g.fillOval((r*sqsz+17), (c*sqsz+17), 16, 16);

            g.setColor(Color.black);

            g.drawOval((r*sqsz+17), (c*sqsz+17), 16, 16);

          }

          g.setColor(Color.black);

          g.drawOval((r*sqsz+10), (c*sqsz+10), 30, 30);

        }

        g.setColor(Color.black);

        g.drawRect((r*sqsz), (c*sqsz), sqsz, sqsz);)}

    g.setColor(Color.black);

    g.drawRect(0, 0, size-1, size-1);}}

/*

  

File: Square.java

Abstract: Model of a single checker square

*/

import java.io.*;

import java.lang.*;

import java.awt.Color;

class Square {

Piece piece;

Color color;

int number;

public Square(Color c, int n){

    color = c;

    number = n;

    piece = null;

}

public void vacate(){

    piece = null;

}

public void arrive(Piece p){

    piece = p;

}

public Color getColor(){

    return color;

}

public int getNumber(){

    return number;

}

public Piece occupant(){

    return piece;

}

}

/*

File: Piece.java

Abstract: Model of a single game piece

*/

import java.io.*;

import java.lang.*;

import java.awt.Color;

class Piece {

Color color;

boolean aking;

public Piece(Color c){

    color = c;

    aking = false;

}

public Color getColor(){

    return color;

}

public boolean isKing(){

    return aking;

}

public void crown(){

    aking = true;

}

}

/*

File: GameSelector.java

Abstract: GUI for selecting the type of game to be played.

*/

import java.io.*;

import java.lang.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

public class GameSelector extends JFrame {

boolean selected;

Container top;

JPanel searcher;

JLabel search;

JTextField levels;

JPanel done;

JButton donebutton;

JPanel light;

ButtonGroup lbuttons;

JRadioButton lhuman;

JRadioButton lrandom;

JRadioButton lmachine;

JPanel dark;

ButtonGroup dbuttons;

JRadioButton dhuman;

JRadioButton drandom;

JRadioButton dmachine;

public GameSelector(){

    super("New Game Selector");

    selected = false;

    top = getContentPane();

    makeSearcher();

    makeDonePanel();

    makeLightPanel();

    makeDarkPanel();

    top.setLayout(new GridLayout(2,2));

    top.add(light);

    top.add(dark);

    top.add(searcher);

    top.add(done);

    setResizable(false);

    pack();

    setVisible(false);

}

private void makeSearcher(){

    searcher = new JPanel();

    search = new JLabel("Search Depth");

    levels = new JTextField("0", 5);

    searcher.add(search);

    searcher.add(levels);

}

private void makeDonePanel(){

    done = new JPanel();

    done.setPreferredSize(new Dimension(10,10));

    donebutton = new JButton("Done");

    done.add(donebutton);

    donebutton.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent evt){

          selected = true;

        }

      });

}

private void makeLightPanel(){

    light = new JPanel();

  lhuman = new JRadioButton("Human Player", true);

    lrandom = new JRadioButton("Random Machine");

    lmachine = new JRadioButton("MiniMax Machine");

    lbuttons = new ButtonGroup();

    lbuttons.add(lhuman);

    lbuttons.add(lrandom);

    lbuttons.add(lmachine);

    light.setLayout(new GridLayout(3,1));

    light.add(lhuman);

    light.add(lrandom);

    light.add(lmachine);

    light.setBorder(new TitledBorder("Light"));

}

private void makeDarkPanel(){

    dark = new JPanel();

    dhuman = new JRadioButton("Human Player", true);

    drandom = new JRadioButton("Random Machine");

    dmachine = new JRadioButton("MiniMax Machine");

    dbuttons = new ButtonGroup();

    dbuttons.add(dhuman);

    dbuttons.add(drandom);

    dbuttons.add(dmachine);

    dark.setLayout(new GridLayout(3,1));

    dark.add(dhuman);

    dark.add(drandom);

    dark.add(dmachine);

    dark.setBorder(new TitledBorder("Dark"));

}

public String newGameSelection(){

    while(!selected){

      //loop waiting for user to select game type

      try{

        Thread.sleep(500);

      }catch(Exception e){ e.printStackTrace(); }

    }

    String newgame = "";

    String num = levels.getText();

    if (num.equals("") || num == null)

      num = "0";

    if (lhuman.isSelected() && dhuman.isSelected())

      newgame = "h-h-game " + num;

    if (lhuman.isSelected() && drandom.isSelected())

      newgame = "h-r-game " + num;

    if (lhuman.isSelected() && dmachine.isSelected())

      newgame = "h-mm-game " + num;

    if (lrandom.isSelected() && dhuman.isSelected())

      newgame = "r-h-game " + num;

    if (lrandom.isSelected() && drandom.isSelected())

      newgame = "r-r-game " + num;

    if (lrandom.isSelected() && dmachine.isSelected())

      newgame = "r-mm-game " + num;

    if (lmachine.isSelected() && dhuman.isSelected())

      newgame = "mm-h-game " + num;

    if (lmachine.isSelected() && drandom.isSelected())

      newgame = "mm-r-game " + num;

    if (lmachine.isSelected() && dmachine.isSelected())

      newgame = "mm-mm-game " + num;

    selected = false;

    return newgame;

}

public static void main (String[] args){

    GameSelector s = new GameSelector();

    s.addWindowListener(new WindowAdapter(){

        public void windowClosing(WindowEvent evt){

          System.exit(0);

        }

      });

    s.setVisible(true);

    System.out.println(s.newGameSelection());

    s.setVisible(false);

    s.setVisible(true);

    System.out.println(s.newGameSelection());

    s.setVisible(false);

    s.setVisible(true);

}

}

Thank you. please upvote

Add a comment
Know the answer?
Add Answer to:
IN JAVA - GAME IS CHECKERS. I AM IN COMP SCIENCE 1 PLEASE DO NOT USE...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game....

    Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...

  • Please help me write a Pseudocode (please do NOT answer in JAVA or Python - I...

    Please help me write a Pseudocode (please do NOT answer in JAVA or Python - I will not be able to use those). Please ALSO create a flowchart using Flowgarithm program. Thank you so much, if answer is provided in Pseudocode and Flowchart, I will provide good feedback and thumbs up to the resonder. Thank you! Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional char array with three rows...

  • Write a class named FBoard for playing a game... PLEASE USE C++ PLEASE DO NOT USE "THIS -->". NOT ALLOWED PLE...

    Write a class named FBoard for playing a game... PLEASE USE C++ PLEASE DO NOT USE "THIS -->". NOT ALLOWED PLEASE PROVIDE COMMENTS AND OUTPUT! Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member...

  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

  • PLEASE INCLUDE SAW-PROMPTS FOR 2 PLAYERS NAMES(VALIDATE NAMES). SHOW MENU (PLAYER MUST SELECT FROM MENU B4...

    PLEASE INCLUDE SAW-PROMPTS FOR 2 PLAYERS NAMES(VALIDATE NAMES). SHOW MENU (PLAYER MUST SELECT FROM MENU B4 THE GAME STARTS 1=PLAY GAME, 2=SHOW GAME RULES, 3=SHOW PLAYER STATISTICS, AND 4=EXIT GAME WITH A GOODBYE MESSAGE.) PLAYERS NEED OPTION TO SHOW STATS(IN A DIFFERNT WINDOW-FOR OPTION 3)-GAME SHOULD BE rock, paper, scissor and SAW!! PLEASE USE A JAVA GRAPHICAL USER INTERFACE. MUST HAVE ROCK, PAPER, SCISSORS, AND SAW PLEASE This project requires students to create a design for a “Rock, Paper, Scissors,...

  • Please help I am struggling with this and 3*Math.random())+1; and how to use it within the...

    Please help I am struggling with this and 3*Math.random())+1; and how to use it within the program For this project you will write a Java program that will play a simple game of Rock, Paper, Scissors.  If you have never played this game before the rules are simple - each player chooses one of Rock, Paper or Scissors and they reveal their choices simultaneously. • The choice of Rock beats the choice of Scissors ("Rock smashes Scissors") • The choice of...

  • Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next...

    Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next two assignments you will be creating a one-player version of the game. The game is extremely simple. Each player arranges a fleet of ships in a grid. The grid is hidden from the opponent. Here is an ASCII representation of a 10x10 grid. The ‘X’s represent ships, the ‘~’s represent empty water. There are three ships in the picture: A vertical ship with a...

  • Please I need your help I have the code below but I need to add one...

    Please I need your help I have the code below but I need to add one thing, after player choose "No" to not play the game again, Add a HELP button or page that displays your name, the rules of the game and the month/day/year when you finished the implementation. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class TicTacToe extends JFrame implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board...

  • Please develop the following code using C programming and using the specific functions, instructi...

    Please develop the following code using C programming and using the specific functions, instructions and format given below. Again please use the functions given especially. Also don't copy any existing solution please write your own code. This is the first part of a series of two labs (Lab 7 and Lab 8) that will complete an implementation for a board-type game called Reversi (also called Othello). The goal of this lab is to write code that sets up the input...

  • Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment...

    Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT