Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import greenfoot.*;
- import java.util.*;
- import java.util.List;
- import java.util.Random;
- // IMPORTANT: If using older version of Greenfoot, uncomment this line.
- // If using newer version, comment out this line.
- //import java.awt.Color;
- /**
- * Snakes in treasures game, in which the player gets point for digging up a pinapple, gets warned or killed if they find a blueberry or is granted immunity if they find a Costa Rican Steve Aronson
- *
- * @author Everett Miceli
- */
- public class Example extends World
- {
- private static final int COLUMNS = 5;
- private static final int ROWS = 5;
- private static final int CELL_SIZE = 100;
- private static final int SKIP_TURN=3;
- private static final int POWER_UP=0;
- private static final int PINEAPPLE=1;
- private static final int BLUEBERRY=2;
- private static int screen[][] = new int[ROWS][COLUMNS];
- private boolean player1Turn=true;
- private static int scores[] = new int[2];
- private static int wins[] = new int[]{0,0};
- private boolean gameOver=false;
- private boolean warning=false;
- private boolean end=false;
- private int difficulty=1;
- private int powerUpChance=10;
- private int pineappleChance=25;
- private int blueberryChance=55;
- private int powerUpLeft=0;
- public Example()
- {
- super(COLUMNS, ROWS, CELL_SIZE);
- setUp();
- }
- /**
- * Sets up the board and resets everything to its original values
- */
- public void setUp()
- {
- player1Turn=true;
- Random rnd = new Random();
- difficulty=getDifficulty();
- checkDifficulty();
- for (int i =0;i<ROWS;i++)
- {
- for (int k =0;k<COLUMNS;k++)
- {
- int g = rnd.nextInt(100);
- if (g<powerUpChance)
- screen[i][k] = 0;
- else if (g>pineappleChance && g<blueberryChance)
- screen[i][k] = 1;
- else
- screen[i][k] = 2;
- }
- }
- screen[ROWS-1][COLUMNS-1] = SKIP_TURN;
- clearBoard();
- drawText("SKIP TURN", 4*CELL_SIZE + CELL_SIZE/2, 4*CELL_SIZE+CELL_SIZE/2);
- scores[0]=0;
- scores[1]=0;
- warning=false;
- gameOver=false;
- }
- /**
- * Checks to see if the player's difficulty input is valid
- */
- public boolean isDiff(String str)
- {
- int[] difficulties = new int[]{1,2,3,4};
- for(int i:difficulties)
- {
- if (str.equals(i))
- return true;
- }
- return false;
- }
- /**
- * Changes who's turn it is
- */
- public void changePlayer()
- {
- if (player1Turn){
- player1Turn=!player1Turn;
- warning=false;
- powerUpLeft=0;
- System.out.println("Player 2's turn");
- }
- else
- gameOver=true;
- }
- /**
- * Gets the difficulty level from the player
- */
- public int getDifficulty()
- {
- String diff = Greenfoot.ask("Pick a number between 1-4");
- if (diff!= null && isDiff(diff))
- return Integer.parseInt(diff);
- return 1;
- }
- /**
- * Clears the board of all pictures
- */
- public void clearBoard()
- {
- GreenfootImage pic = new GreenfootImage("white.jpg");
- for (int i =0;i<ROWS;i++)
- {
- for (int k =0;k<COLUMNS;k++)
- {
- getBackground().drawImage(pic, i*CELL_SIZE, k*CELL_SIZE);
- }
- }
- }
- /**
- * Checks the current difficulty level and changes the chances of each item.
- */
- public void checkDifficulty()
- {
- if (difficulty==4)
- {
- pineappleChance = 40;
- blueberryChance=50;
- }
- else if (difficulty==3)
- {
- pineappleChance=35;
- blueberryChance=55;
- }
- else if (difficulty==2)
- {
- pineappleChance=30;
- blueberryChance=60;
- }
- }
- /**
- * Gets the player's mouse position and flips over the card at that location. Mouse null position checker by Super_Hippo on greenfoot.org
- */
- public void act()
- {
- if (gameGoing()){
- MouseInfo x =Greenfoot.getMouseInfo();
- if (Greenfoot.mouseClicked(this) && x!=null) {
- System.out.println(gameOver);
- int row = Greenfoot.getMouseInfo().getY();
- int col = Greenfoot.getMouseInfo().getX();
- int loc = screen[row][col];
- if (loc==SKIP_TURN){
- System.out.println("Turn skipped");
- player1Turn=!player1Turn;
- }
- else if (loc == -1)
- {
- drawText("You already dug here", row*CELL_SIZE + CELL_SIZE/2, col*CELL_SIZE+CELL_SIZE/2);
- }
- else if (loc==POWER_UP){
- drawPic(row, col,"steve.png");
- powerUpLeft+=2;
- }
- else if (loc==PINEAPPLE){
- drawPic(row, col,"pa.png");
- Greenfoot.playSound("success.wav");
- updateScore();
- }
- else {
- drawPic(row, col,"bb.png");
- if (powerUpLeft<1)
- updateWarnings();
- else
- powerUpLeft--;
- }
- if(loc!=3)
- screen[row][col] = -1;
- }
- }
- end();
- }
- /**
- * Ends the game
- */
- public void end()
- {
- if (end){
- System.out.println("Game over");
- if(scores[0] > scores[1]){
- System.out.println("Player 1 wins");
- wins[0]++;
- }
- else{
- System.out.println("Player 2 wins");
- wins[1]++;
- }
- System.out.println("Player 1 has " + wins[0] + " wins." + " Player 2 has " + wins[1] + " wins.");
- end=false;
- setUp();
- }
- }
- /**
- * Updates the warning system and kills the player if they already have one
- */
- public void updateWarnings()
- {
- if (warning && !player1Turn){
- gameOver=true;
- scores[1]=0;
- }
- else if (warning && player1Turn){
- player1Turn = !player1Turn;
- scores[0]=0;
- warning=false;
- }
- else
- warning=true;
- }
- /**
- * Checks to see if the game is still going
- */
- public boolean gameGoing()
- {
- if (allFlipped() || gameOver){
- end=true;
- return false;
- }
- return true;
- }
- /**
- * Checks to see if all of the cards have been flipped over
- */
- public boolean allFlipped()
- {
- for (int i =0;i<ROWS;i++)
- {
- for (int k =0;k<COLUMNS;k++)
- {
- if (screen[i][k]!=-1)
- return false;
- }
- }
- return true;
- }
- /**
- * Skips the players turn or ends the game if it is player 2's turn
- */
- public void skipTurn()
- {
- System.out.println("Turn skipped");
- changePlayer();
- warning=false;
- }
- /**
- * Updates the player's score
- */
- private void updateScore()
- {
- if(player1Turn)
- scores[0]++;
- else
- scores[1]++;
- System.out.println("p1 " + scores[0] + "p2 " + scores[1]);
- }
- /**
- * Draw text in a given cell
- */
- public void drawText(String str, int row, int col)
- {
- GreenfootImage imageText = new GreenfootImage(str, 10, Color.BLACK, Color.RED);
- getBackground().drawImage(imageText, col, row);
- }
- /**
- * Draws a picture
- */
- public void drawPic(int row, int col, String name)
- {
- GreenfootImage pic = new GreenfootImage(name);
- pic.scale(CELL_SIZE,CELL_SIZE);
- getBackground().drawImage(pic, col*CELL_SIZE, row*CELL_SIZE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement