You are to create a “Game of college” that models itself after the Game of Life.
ID: 3814390 • Letter: Y
Question
You are to create a “Game of college” that models itself after the Game of Life. Using JAVA
Some classes to help
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GameBoard extends JApplet implements ActionListener{
double money = 1000;
public void init (){
Container contentPane = getContentPane();
contentPane.setBackground(Color.white);
contentPane.setLayout(new FlowLayout());
//buttons
//Page 464 in book!!!!
JButton payDay = new JButton("Pay Day!");
contentPane.add(payDay);
payDay.addActionListener(this);
JButton jobPay = new JButton("Get your check");
contentPane.add(jobPay);
jobPay.addActionListener(this);
JButton ticket = new JButton("Drinking ticket, pay up!");
contentPane.add(ticket);
ticket.addActionListener(this);
JButton aid = new JButton("You've gotten financial aid!");
contentPane.add(aid);
aid.addActionListener(this);
JButton buyTextbooks = new JButton("Buy your textbooks!");
contentPane.add(buyTextbooks);
buyTextbooks.addActionListener(this);
/*JButton pickGoodCard = new JButton("Pick a card from the good deck");
contentPane.add(pickGoodCard);
JButton pickBadCard = new JButton("Pick a card from the bad deck");
contentPane.add(pickBadCard);
JButton moveBack = new JButton("Move back a random number of spaces!");
contentPane.add(moveBack);*/
}
public void actionPerformed(ActionEvent e){
Container contentPane = getContentPane();
if(e.getActionCommand().equals("Pay Day!")){
money+= 200;
JOptionPane.showMessageDialog(null, "You have earned $200!");
}
else if(e.getActionCommand().equals("Get your check")){
money+= 100;
JOptionPane.showMessageDialog(null, "You have earned a $100 check!");
}
else if (e.getActionCommand().equals("Drinking ticket, pay up!")){
money-= 175;
JOptionPane.showMessageDialog(null, "BUSTED! Pay your drinking ticket.");
}
else if (e.getActionCommand().equals("You've gotten financial aid!")){
money+= 500;
JOptionPane.showMessageDialog(null, "Awarded financial aid, collect your $500!");
}
else if (e.getActionCommand().equals("Buy your textbooks!")){
money-= 400;
JOptionPane.showMessageDialog(null, "You have to get textbooks! The price is $400");
}
else{
System.out.println("Error in button interface.");
}
}
}
public class Player {
private static int currentPlayer;
private String playerName;
private int position;
private int money;
public Player()
{
this.playerName = "";
this.position = 1;
this.money = 200;
}
public Player(String name)
{
this.playerName= name;
}
public static int getCurrentPlayer() {
return currentPlayer;
}
public static void setCurrentPlayer(int currentPlayer) {
Player.currentPlayer = currentPlayer;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
The entire game must be completed electronically, which means the spin of the wheel, keeping track of money and all other aspects must be simulated through the computer. You can modify the game board in any way you like, particularly to decrease the number of spaces. However, there must be no less than 20 spaces and the number of spaces must be appropriate for the game play.
For your version of the game, you will have spaces on the game board that represent your life as a student at college. I would suggest you consider financial aid, registration, housing, job, classes, faculty, textbooks, social activities, including sports, fraternities, volunteering, etc. You will complete this assignment in groups of three or four. Your grade will be based on the following:
Experience: 0 - 25 points- Did you capture the college experience?
Creativity: 0 – 25 points
Design of game board: 0 – 25 points – Is the game board colorful and easy to navigate?
Game play: 0 - 50 points – Are you able to follow the game play and know what is going on and the status of each player? Can you tell where each play is residing on the game board? There must be a clear winner and the game must stop once a winner is determined?
Spin of wheel: 0 -25 points – Is there some way to simulate the spin of the wheel? If there is an actual visual of a wheel spinning, then you will get the full 20 points. If it is simulated behind the scenes but the results appear, then you will earn 10 points.
Cards: 0 -25 points- Is there a random representation of the cards being played?
Payday – 0 - 25 points- Is there a way to earn money and to lose money? Are the totals for each player displayed and able to be retrieved easily?
Explanation / Answer
import java.util.*;
import edu.princeton.cs.introcs.StdDraw;
public class GameOfLife
{
public static void main(String[] args)
{
// the size of cells' array final int ROWS_NUM = 500;
final int COLS_NUM = 500;
Boolean[][] curGen = new Boolean[ROWS_NUM][COLS_NUM];
// sets dead cells array
for (int row = 0; row < ROWS_NUM; row++)
{
Arrays.fill(curGen[row], false);
}
// sets initial pattern - glider //
curGen[149][150] = true;
// curGen[150][151] = true;
// curGen[151][149] = true;
// curGen[151][150] = true;
// curGen[151][151] = true;
// fills array with random booleans, veeery slow
Random random = new Random();
for (int row = 0; row < ROWS_NUM; row++)
{
for (int col = 0; col < COLS_NUM; col++)
{
curGen[row][col] = random.nextBoolean();
}
}
// initialises field for drawing cells
StdDraw.setCanvasSize(COLS_NUM, ROWS_NUM);
StdDraw.setYscale(0, ROWS_NUM);
StdDraw.setXscale(0, COLS_NUM);
StdDraw.setPenRadius(0);
StdDraw.setPenColor(StdDraw.BLACK);
// infinitely draws field
while (true)
{
curGen = countNextGen(curGen, ROWS_NUM, COLS_NUM);
StdDraw.clear();
for (int row = 0; row < ROWS_NUM; row++)
{
for (int col = 0; col < COLS_NUM; col++)
{
if (curGen[row][col] == true)
{
StdDraw.point(col, row);
}
}
}
}
}
// counts next generation
public static Boolean[][] countNextGen(Boolean[][] curGen, int rowsNum, int colsNum)
{
// copies the current array of cells into temporary array so grid can
// be changed without affecting the other cells
Boolean[][] nextGen = new Boolean[rowsNum][];
for (int row = 0; row < rowsNum; row++)
{
nextGen[row] = Arrays.copyOf(curGen[row], colsNum);
}
// decides what will happen to cell
for (int row = 0; row < rowsNum; row++)
{
for (int col = 0; col < colsNum; col++)
{
int numOfNeighbours = countCellNeighbours(curGen, rowsNum, colsNum, row, col);
// under or overpopulation, cell dies
if ((numOfNeighbours < 2) || (numOfNeighbours > 3))
{
nextGen[row][col] = false;
}
// cell lives on to next generation
if (numOfNeighbours == 2)
{
nextGen[row][col] = curGen[row][col];
}
// cell either stays alive, or is born
if (numOfNeighbours == 3)
{
nextGen[row][col] = true;
}
}
}
return nextGen;
}
// counts cell's neighbours
public static int countCellNeighbours(Boolean[][] curGen, int rowsNum, int colsNum, int row, int col)
{
int numOfNeighbours = 0;
// decides which neighbour cells to count (for edge cells
// checks for neighbours from opposite edge)
// not edge cells
if ((row > 0) && (row < rowsNum - 1) && (col > 0) && (col < colsNum - 1))
{
if (curGen[row - 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row - 1][col])
{
numOfNeighbours++;
}
if (curGen[row - 1][col + 1])
{
numOfNeighbours++;
}
if (curGen[row][col - 1])
{
numOfNeighbours++;
}
if (curGen[row][col + 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][col])
{
numOfNeighbours++;
}
if (curGen[row + 1][col + 1])
{
numOfNeighbours++;
}
}
// top cells
else if (row == 0)
{
// top-left cells
if (col == 0)
{
// above
if (curGen[rowsNum - 1][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[rowsNum - 1][col])
{
numOfNeighbours++;
}
if (curGen[rowsNum - 1][col + 1])
{
numOfNeighbours++;
}
// same row
if (curGen[row][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row][col + 1])
{
numOfNeighbours++;
}
// below
if (curGen[row + 1][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][col])
{
numOfNeighbours++;
}
if (curGen[row + 1][col + 1])
{
numOfNeighbours++;
}
}
// top-right cells
else if (col == colsNum - 1)
{
// above
if (curGen[rowsNum - 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[rowsNum - 1][col])
{
numOfNeighbours++;
}
if (curGen[rowsNum - 1][0])
{
numOfNeighbours++;
}
// same row
if (curGen[row][col - 1])
{
numOfNeighbours++;
}
if (curGen[row][0])
{
numOfNeighbours++;
}
// below
if (curGen[row + 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][col])
{
numOfNeighbours++;
}
if (curGen[row + 1][0])
{
numOfNeighbours++;
}
}
// top but not left or right else
{
// above
if (curGen[rowsNum - 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[rowsNum - 1][col])
{
numOfNeighbours++;
}
if (curGen[rowsNum - 1][col + 1])
{
numOfNeighbours++;
}
// same row
if (curGen[row][col - 1])
{
numOfNeighbours++;
}
if (curGen[row][col + 1])
{
numOfNeighbours++;
}
// below
if (curGen[row + 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][col])
{
numOfNeighbours++;
}
if (curGen[row + 1][col + 1])
{
numOfNeighbours++;
}
}
}
//bottom cells
else if (row == rowsNum - 1)
{
// bottom-left cells
if (col == 0)
{
// above
if (curGen[row - 1][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row - 1][col])
{
numOfNeighbours++;
}
if (curGen[row - 1][col + 1])
{
numOfNeighbours++;
}
// same row
if (curGen[row][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row][col + 1])
{
numOfNeighbours++;
}
// below
if (curGen[0][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[0][col])
{
numOfNeighbours++;
}
if (curGen[0][col + 1])
{
numOfNeighbours++;
}
}
// bottom-right cells
else if (col == colsNum - 1)
{
// above
if (curGen[row - 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row - 1][col])
{
numOfNeighbours++;
}
if (curGen[row - 1][0])
{
numOfNeighbours++;
}
// same row
if (curGen[row][col - 1])
{
numOfNeighbours++;
}
if (curGen[row][0])
{
numOfNeighbours++;
}
// below if (curGen[0][col - 1])
{
numOfNeighbours++;
}
if (curGen[0][col])
{
numOfNeighbours++;
}
if (curGen[0][0])
{
numOfNeighbours++;
}
}
// bottom but not left or right else
{
// above
if (curGen[row - 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row - 1][col])
{
numOfNeighbours++;
}
if (curGen[row - 1][col + 1])
{
numOfNeighbours++;
}
// same row
if (curGen[row][col - 1])
{
numOfNeighbours++;
}
if (curGen[row][col + 1])
{
numOfNeighbours++;
}
// below
if (curGen[0][col - 1])
{
numOfNeighbours++;
}
if (curGen[0][col])
{
numOfNeighbours++;
}
if (curGen[0][col + 1])
{
numOfNeighbours++;
}
}
}
// left but not top or bottom cells
else if (col == 0)
{
// above
if (curGen[row - 1][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row - 1][col])
{
numOfNeighbours++;
}
if (curGen[row - 1][col + 1])
{
numOfNeighbours++;
}
// same row
if (curGen[row][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row][col + 1])
{
numOfNeighbours++;
}
// below
if (curGen[row + 1][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][col])
{
numOfNeighbours++;
}
if (curGen[row + 1][col + 1])
{
numOfNeighbours++;
}
}
// right but not top or bottom cells
else if (col == colsNum - 1)
{
// above
if (curGen[row - 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row - 1][col])
{
numOfNeighbours++;
}
if (curGen[row - 1][0])
{
numOfNeighbours++;
}
// same row
if (curGen[row][col - 1])
{
numOfNeighbours++;
}
if (curGen[row][0])
{
numOfNeighbours++;
}
// below
if (curGen[row + 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][col])
{
numOfNeighbours++;
}
if (curGen[row + 1][0])
{
numOfNeighbours++;
}
}
return numOfNeighbours;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.