Commit da360d90 authored by Felix RABADAN's avatar Felix RABADAN
Browse files

fix scanner bug

parent 198442ab
......@@ -8,8 +8,9 @@ import java.util.Scanner;
import game.Player.Position;
public class Game {
static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("1 - Normal game\n2 - Test game");
int i = sc.nextInt();
if (i == 1) {
......@@ -20,7 +21,6 @@ public class Game {
}
private static void normalGame() {
Scanner sc = new Scanner(System.in);
Boolean end = false;
while (!end) {
UserController userController = UserController.getInstance();
......@@ -31,6 +31,7 @@ public class Game {
System.out.println("Name of the new user :");
String name = sc.next();
userController.connect(userController.addUser(name));
connected();
break;
case 2:
System.out.println("Name of the user");
......@@ -40,7 +41,6 @@ public class Game {
break;
case 3:
admin();
sc.next();
break;
case 4:
System.out.println("Name of the user you want to remove :");
......@@ -53,11 +53,9 @@ public class Game {
break;
}
}
sc.close();
}
private static void connected() {
Scanner sc = new Scanner(System.in);
User u = UserController.getInstance().getConnectedMember();
Boolean connected = true;
while (connected) {
......@@ -85,11 +83,9 @@ public class Game {
break;
}
}
sc.close();
}
private static void market() {
Scanner sc = new Scanner(System.in);
User u = UserController.getInstance().getConnectedMember();
Boolean onMarket = true;
while (onMarket) {
......@@ -112,12 +108,10 @@ public class Game {
break;
}
}
sc.close();
}
private static void admin() {
Admin admin = Admin.getInstance();
Scanner sc = new Scanner(System.in);
Boolean connected = true;
while (connected) {
System.out.println("1 - new file\n2 - new card(s)\n3 - new week\n4 - disconnect");
......@@ -149,12 +143,12 @@ public class Game {
break;
case 4:
connected = false;
sc.nextLine();
break;
default:
break;
}
}
sc.close();
}
private static void testGame() {
......
......@@ -2,7 +2,6 @@ package game;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;
import game.Card.Rarity;
import game.Player.Position;
......@@ -12,8 +11,6 @@ public class User extends Member {
private Card[] team;
private Float teamScore =(float) 0;
private Scanner scanner = new Scanner(System.in);
public User(String name) {
super(name);
this.balance = 10000;
......@@ -62,12 +59,12 @@ public class User extends Member {
do {
try {
do {
choice = scanner.nextInt();
choice = Game.sc.nextInt();
} while (choice >= deck.size());
error = false;
} catch (Exception e) {
System.err.println("Error : enter a number");
scanner.next();
Game.sc.next();
}
} while (error);
return deck.get(choice);
......@@ -79,7 +76,7 @@ public class User extends Member {
Player.Position chosePosition;
boolean samePlayer;
do {
choice = scanner.nextInt();
choice = Game.sc.nextInt();
chosePosition = deck.get(choice).getPlayer().getPosition();
samePlayer = false;
for (Card card : team) {// check si le joueur nest pas deja dans la team
......@@ -117,7 +114,7 @@ public class User extends Member {
displayDeck();
Card chosenCard = chooseCard();
System.out.println("At which price ?");
int price = scanner.nextInt();
int price = Game.sc.nextInt();
putOnMarket(new Triple<Card, Integer, User>(chosenCard, price, this));
System.out.println("Your card is on the market now");
deck.remove(chosenCard);
......@@ -130,11 +127,11 @@ public class User extends Member {
if (!Market.getInstance().getBuyableCards().isEmpty()) {
System.out.println("Which card would you like to buy ?");
Market.getInstance().displayBuyableCards();
int choice = scanner.nextInt();
int choice = Game.sc.nextInt();
Triple<Card, Integer, User> chosenCard = Market.getInstance().getBuyableCards().get(choice);
int price = chosenCard.getSnd();
System.out.println("It will costs " + price + " coins\nAre you sure ? (yes/no)");
String confirm = scanner.next();
String confirm = Game.sc.next();
if (confirm.equals("yes")) {
if (balance >= price) {
balance -= chosenCard.getSnd();
......@@ -157,11 +154,11 @@ public class User extends Member {
System.out.println("Which card would you like to exchange ?");
displayDeck();
Card chosenCard = chooseCard();
scanner.nextLine(); // it consumes the \n character
Game.sc.nextLine(); // it consumes the \n character
System.out.println("With which card ?\nName of the player :");
String namePlayer = scanner.nextLine();
String namePlayer = Game.sc.nextLine();
System.out.println("Rarity (common/less_common/rare) :");
String str_rarity = scanner.nextLine();
String str_rarity = Game.sc.nextLine();
Rarity rarity = Rarity.valueOf(str_rarity.toUpperCase()); // convert String to enum
Pair<String, Rarity> mapValue = new Pair<String, Rarity>(namePlayer, rarity);
if (!Market.getInstance().getExchangeableCards().isEmpty()) {
......
......@@ -5,7 +5,6 @@ import java.util.Collections;
import java.util.HashMap;
import game.Card;
import game.Member;
import game.User;
public final class UserController {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment