Oi pessoal,
eu estou criando um jogo de batalha naval para a cadeira de programacão na faculdade. Eu ainda tenho um nivel muito basico então estou com algumas dificuldades.
O jogo precisa das seguintes coisas:
Tem que conter 3 classes: Game, GameBoard e Player
Entrar número de jogadores de 1 a 4
Entrar para cada jogador suas informacões pessoais (nome, idade e email)
Gerar um tabuleiro que o usuario define quantas colunas por quantas linhas terà (min. 10 - max. 20)
Gerar randonicamente um barco que tem que ter o tamanho de 1/3 do tamanho das colunas.
Cada jogador tenta uma jogada e acertando ou errando passa para o proximo jogador
Tem que manter os acertos e erros de cada jogador e com essas informacões manter um score para cada, printando quem ganhou no fim.
Bom, na class palyer eu criei um metodo para o numero de jogadores, um metodo para cada informacao requerida e um metodo para dar loop nas informacoes e um main method. No main method eu chamo o metodo numberOfPlayers(); e addArrayList();
Meu problema é que todas as vezes que roda o loop que está dentro do addArrayList(); aparece uma frase do metodo numberOfPlayers(); que não está em loop.
package Battleship;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
/*
* ========================================
*
* Student: Andressa Martins Schinoff Alves
* Student number: 2017280
* Teacher: Ken Healy
*
* ========================================
*/
public class player {
//Score global variable
int playerScore;
//Save age into a integer
int age;
//Name global variable that contains substrings firstName and surname
String name = "", firstName = "", surname = "";
//Boolean to look at if it has spaces
boolean spaceUsed;
//boolean to look at if it has @
boolean validAT;
//boolean to look if it has .com
boolean validDotCom;
//boolean to look if it has .ie
boolean validDotIE;
//It
int spaceAt;
//Email global variable
String email;
//Save the input of how many players will play into a int
int howManyPlayers;
//This variable is to add which loop and change the player later
int player = 1;
/*this method is run methods numberOfPlayers, name, email and age after run it
* save the player info a array list, also after it is in loop
* to run how many times it is request.*/
public player(String name, int age, String email) {
//calls method number of players
numberOfPlayers();
addArrayList();
this.name = name;
this.age = age;
this.email = email;
}
//I made this method to add objects to array list
//and do the for loop for each player
public void addArrayList() {
//Using for loop for run and save it for each player
//int i starts on 1 to use also as number of the player on the screen
for(int i = 1; i < howManyPlayers+1; i++) {
//Print on the screen what the user should insert
//I used the int i to clarify for the user which information is being request
System.out.println("Insert player " + i + " details");
//call methods for save it for each player
name();
age();
email();
ArrayList<player> myplayer = new ArrayList<player>();
myplayer.add(new player(name,age,email));
}
}
public void name() {
//boolean value to validate
boolean valid = false;
do {
//Read the input
Scanner fullName = new Scanner(System.in);
//Print in the screen which input the user should insert
System.out.println("What is your full name?");
//Save the input as a variable
name = fullName.nextLine();
//It is to eliminate the space the user can insert for accident, but consider the space between words
name = name.trim();
//It is to find the space between first name and surname
spaceUsed = name.contains(" ");
//I used the if Statement to do 2 things first to valid the input
//but also to recognise the first name and surname
if (spaceUsed) {
valid = true;
//It is used to recognise the space in the variable, it could be any type of variable
spaceAt = name.indexOf(' ');
//Recognise everything is insert before the first space and becomes it into a new variable
firstName = name.substring(0,spaceAt);
//Take everything after the first space and becomes it into another variable
surname = name.substring(spaceAt+1,name.length());
//RETIRAR ISSO
System.out.println("Backwards, your name is: " + surname + " " + firstName);
//if it not have first name and surname it's not valid
}else {
valid = false;
//Print in the screen the error message, helping the user solve the problem
System.out.println("Please, enter your first name and surname.");
}
//Anything else is not valid
}while(valid == false);
}
public int age() {
//Boolean to valid
boolean valid = false;
do {
//Scanner is used to read the input
Scanner ageInput = new Scanner(System.in);
//Print what input the user should insert
System.out.println("What is your age?");
//input read equals variable age
age = ageInput.nextInt();
//if statement used to
if(age >= 12 && age <= 100) {
valid = true;
}
else {
valid = false;
System.out.println("Sorry you need have between 12 and 100 to play.");
}
}
while(valid == false);
return age;
}
public void email() {
boolean valid = false;
do {
//Read the user input
Scanner emailInput = new Scanner(System.in);
//Print on screen what input the user should insert
System.out.println("What is your email?");
//save the user input as a variable email
email = emailInput.nextLine();
//I used 3 variables to look if the variable email contains "@", ".com" and ".ie"
//I don`t know if has how look the 3 info in the same variable
//But it was the way I find to validate correctly
validAT = email.contains("@");
validDotCom = email.contains(".com");
validDotIE = email.contains(".ie");
//Use the if statement to validate
//it`s valid just if it has @ and .com OR @ and .ie
if(validAT && (validDotCom || validDotIE)) {
valid = true;
}
//Any email the don't contains @ or .com/.ie is not valid
else {
valid = false;
System.out.println("Please, enter a valid email.");
}
}
while(valid == false);
}
public int score() {
//Score = Hit() - (Misses() * 2)
return playerScore;
}
public int Hit() {
int hits = 0;
int Hit = 0;
hits++;
return Hit;
}
//I use the method int to returns the number of players to use later in another class
public int numberOfPlayers(){
//boolean to validate how many players since it can have min.1 and max.4 players
boolean valid = false;
//Prints on the screen what the user should insert
System.out.println("How many players will play? (Min.1 - Max.4)");
do {
Scanner players = new Scanner(System.in);
howManyPlayers = players.nextInt();
if(howManyPlayers >= 1 && howManyPlayers <= 4) {
valid = true;
}else {
valid = false;
System.out.println("Please, enter a valid number.");
}
}while(valid == false);
return howManyPlayers;
}
}
package Battleship;
/*
* ========================================
*
* Student: Andressa Martins Schinoff Alves
* Student number: 2017280
* Teacher: Ken Healy
*
* ========================================
*/
public class Game {
public static void main(String[] args) {
// TODO code application logic here
String name = "";
int age = 0;
String email = "";
int row = 0;
int col = 0;
//player Player = new player(name, age, email);
GameBoard Battleship = new GameBoard(row, col);
}
}