Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pokemon;
- import java.io.IOException;
- public class Game {
- public static enum Type {
- FIRE, WATER, GRASS, GROUND, ELECTRIC;
- boolean Advantage(Type t){
- if(this == FIRE)
- return t == GRASS;
- else if(this == WATER)
- return (t == FIRE || t==GROUND);
- else if(this == GRASS)
- return (t == WATER || t==GROUND);
- else if(this == GROUND)
- return (t == ELECTRIC);
- else if(this == ELECTRIC)
- return t == WATER;
- return false;
- }
- }
- public Player one;
- public Player two;
- private Player[] players;
- PokeIO io;
- private int turn;
- /*
- * choosePlayer = gets the user to choose a player
- *@no params
- *@return a player depending on which input - 0 for P1, 1 for P2 - the player returns*/
- public Player choosePlayer(){
- Player p = null;
- while(p == null){
- System.out.println("Choose a player by their index");
- for(int i=0;i<players.length;i++){
- System.out.println(i+":"+players[i].name+", ");
- }
- try{
- String strindex = io.getNextInput();
- p = players[Integer.parseInt(strindex)];
- }catch(NumberFormatException e){
- System.out.println("Please type in a number next time...*sigh*");
- }catch(IOException e){
- System.out.println("IOEXception!");
- }catch(ArrayIndexOutOfBoundsException e){
- System.out.println("That isn't a valid player index, try again");
- }
- }
- return p;
- }
- /*
- * chooseFieldPoke = chooses a Pokemon of each player
- *@param p: a player
- *@returns a pokemon based on which number the user chose*/
- public Pokemon chooseFieldPoke(Player p){
- Pokemon m = null;
- while (m == null){
- int index = choose(p);
- System.out.println("chose " + index);
- if(index == -1)
- return p.field.battlestation;
- try{
- return p.field.bench[index];}
- catch(ArrayIndexOutOfBoundsException e){
- System.out.println("not a bench position!");}
- }
- return m;
- }
- /*
- * choose = takes in a player and returns an integer input (which the user gives into the IO method)
- *@param p: a player
- *@returns the int the player chose: is used for chooseFieldPoke*/
- public int choose(Player p){
- System.out.println("Choose Pokemon (index), -1 is the index for battlestation");
- int choice = -2;
- while(choice < -1 || choice >= p.field.bench.length){
- try{
- String strindex = io.getNextInput();
- return Integer.parseInt(strindex);
- }catch(NumberFormatException e){
- System.out.println("Please type in a number next time...*sigh*");
- }catch(IOException e){
- System.out.println("IOEXception!");
- }
- }
- return choice;
- }
- /*
- * Game = takes in 2 player names, and 2 decks
- *@param name1: player1 name
- * *@param name2: player2 named
- * *@param deck1: player1 deck
- * *@param deck2: player2 deck
- *@initialises the game, returns the object of game. is the constructor*/
- public Game(String name1, String name2, String deck1, String deck2){
- one = new Player(name1,Deck.createDeck(deck1));
- two = new Player(name2,Deck.createDeck(deck2));
- players = new Player[2];
- players[0] = one;
- players[1]= two;
- io = new PokeIO();
- turn = 0;
- /*
- * nextTurn = goes to the next turn
- *@no params
- *@returns the next turn*/
- }
- private void nextTurn(){
- turn = (turn+1)%(players.length);
- }
- /*
- * getNextPlayer = goes to the next turn
- *@no params
- *@returns the next turn*/
- public Player getNextPlayer(){
- return players[(turn+1)%players.length];
- }
- /*
- * gets the current player
- * @return Player, the current player of the game*/
- public Player getPlayer(){
- return players[turn];
- }
- /*
- * prints the fields of both players
- * @return void
- * */
- private void printFields(){
- for(Player p : players){
- System.out.println(p.name+":");
- p.field.printField();
- }
- }
- /*
- * prints the hands of both the players
- * @return void
- * */
- private void printHands(){
- for(Player p : players){
- System.out.print(p.name+":");
- p.printHand();
- System.out.println(p.name+" has "+p.deck.size()+" cards in the deck");
- }
- }
- /*
- * uses the given card in a game
- * @param index, the int of the index of the current player's hand where the card resides
- * @return void, mutates the game so the cards effect is felt
- * */
- private void playCard(int index){
- if(getPlayer().hand.isEmpty())
- System.out.println("You have no cards left!");
- else
- getPlayer().useCard(index, this);
- }
- /*
- * the current player attack the next player
- * if an attack is possible
- * @return boolean, whether or not the player has won(attacked an empty battlefield)
- * */
- private boolean attack(){
- if(getNextPlayer().field.battlestation == null){
- System.out.println(players[turn].name+ " WINS!");
- return true;
- }
- else{
- Pokemon opponent = getNextPlayer().field.battlestation;
- if(getPlayer().field.battlestation.attack(opponent))
- getNextPlayer().field.battlestation = null;
- }
- return false;
- }
- /*
- * runs a turn of the game
- * @return boolean, whether or not the game is over
- * */
- private boolean turn(){
- getPlayer().draw();
- String action = "";
- while(! (action.equals("pass") || action.equals("attack"))){
- try{
- System.out.print(players[turn].name+":");
- action = io.getNextInput();
- String[] words = action.split(" ");
- if(action.equals("print field"))
- printFields();
- else if(action.equals("print hand"))
- printHands();
- else if(words[0].equals("play") && words.length>1){
- try{playCard(Integer.parseInt(words[1]));}
- catch(NumberFormatException e){
- System.out.println("didn't type in a valid number");}
- }
- else if(words[0].equals("switch")&& words.length>1){
- try{
- getPlayer().switchPoke(Integer.parseInt(words[1]));
- }catch(NumberFormatException e){
- System.out.println("Please type in a number next time...*sigh*");
- }
- }
- else if(action.equals("attack")){
- if(getPlayer().field.hasAttackable()){
- if(attack()){return true;};
- }
- else{
- action = "";//so we don't end the turn'
- System.out.println("You don't have an attackable pokemon in your battlestation");
- }
- }
- }catch(IOException e){
- System.out.println("weird input, try again!");
- }
- }
- getPlayer().endTurn();
- nextTurn();
- return false;
- }
- /*
- * plays a given game
- * @return void
- * */
- public void playGame(){
- boolean done = false;
- while (done == false){
- done = turn();
- }
- }
- public static void main(String[] args){
- Game g = new Game("Charlie","Kenneth","cards","cards");
- g.playGame();
- }
- }
Add Comment
Please, Sign In to add comment