Restarting a while loop from the beginning / java - java

I have a game of tic tac toe that I need to play all the way through and then ask the user if they would like to play again.
I'm not sure how I would go about clearing the array and starting from the beginning of the loop.
I have a method at the very bottom of the code that I was thinking would possibly work but haven't had much luck with it.
Any help would be amazing thanks!
public class TicTacToe {
public static char[][] board = new char[3][3];
static Scanner scan = new Scanner(System.in);
public static String keepPlaying;
public static char yesOrNo;
public static void main(String[] args) {
User human1 = new User();
User human2 = new User();
human1.getUser1Name();
human2.getUser2Name();
System.out.println("\nPlayer 1: " + human1.human1Name() + ": X's");
System.out.println("Player 2: " + human2.human2Name() + ": O's\n");
initializeBoard();
displayBoard();
boolean playing = true;
while (playing) {
System.out.println("\n" + human1.human1Name() + " it's your turn.");
human1.getChoice();
human1.convert();
if (human1.returnRow() == 0 && human1.returnCol() == 0 && board[0][0] == ' ') {
board[0][0] = 'X';
displayBoard();
} else if (human1.returnRow() == 0 && human1.returnCol() == 1 && board[1][0] == ' ') {
board[1][0] = 'X';
displayBoard();
} else if (human1.returnRow() == 0 && human1.returnCol() == 2 && board[2][0] == ' ') {
board[2][0] = 'X';
displayBoard();
} else if (human1.returnRow() == 1 && human1.returnCol() == 0 && board[0][1] == ' ') {
board[0][1] = 'X';
displayBoard();
} else if (human1.returnRow() == 1 && human1.returnCol() == 1 && board[1][1] == ' ') {
board[1][1] = 'X';
displayBoard();
} else if (human1.returnRow() == 1 && human1.returnCol() == 2 && board[2][1] == ' ') {
board[2][1] = 'X';
displayBoard();
} else if (human1.returnRow() == 2 && human1.returnCol() == 0 && board[0][2] == ' ') {
board[0][2] = 'X';
displayBoard();
} else if (human1.returnRow() == 2 && human1.returnCol() == 1 && board[1][2] == ' ') {
board[1][2] = 'X';
displayBoard();
} else if (human1.returnRow() == 2 && human1.returnCol() == 2 && board[2][2] == ' ') {
board[2][2] = 'X';
displayBoard();
}
checkForWinner();
if (checkForWinner() == true) {
System.out.println("\n" + human1.human1Name() + " has won the game!!!");
break;
}
if (board[0][0] != ' ' && board[0][1] != ' ' && board[0][2] != ' ' && board[1][0] != ' '
&& board[1][1] != ' ' && board[1][2] != ' ' && board[2][0] != ' ' && board[2][1] != ' '
&& board[2][2] != ' ') {
System.out.println("It's a draw!!!");
break;
}
System.out.println("\n" + human2.human2Name() + " it's your turn.");
human2.getChoice();
human2.convert();
if (human2.returnRow() == 0 && human2.returnCol() == 0/* && board[0][0] == ' ' */) {
if (board[0][0] != ' ') {
System.out.println("\n" + human2.human2Name() + " that spot is already taken.");
human2.getChoice();
human2.convert();
continue;
}
board[0][0] = 'O';
displayBoard();
} else if (human2.returnRow() == 0 && human2.returnCol() == 1 && board[1][0] == ' ') {
board[1][0] = 'O';
displayBoard();
} else if (human2.returnRow() == 0 && human2.returnCol() == 2 && board[2][0] == ' ') {
board[2][0] = 'O';
displayBoard();
} else if (human2.returnRow() == 1 && human2.returnCol() == 0 && board[0][1] == ' ') {
board[0][1] = 'O';
displayBoard();
} else if (human2.returnRow() == 1 && human2.returnCol() == 1 && board[1][1] == ' ') {
board[1][1] = 'O';
displayBoard();
} else if (human2.returnRow() == 1 && human2.returnCol() == 2 && board[2][1] == ' ') {
board[2][1] = 'O';
displayBoard();
} else if (human2.returnRow() == 2 && human2.returnCol() == 0 && board[0][2] == ' ') {
board[0][2] = 'O';
displayBoard();
} else if (human2.returnRow() == 2 && human2.returnCol() == 1 && board[1][2] == ' ') {
board[1][2] = 'O';
displayBoard();
} else if (human2.returnRow() == 2 && human2.returnCol() == 2 && board[2][2] == ' ') {
board[2][2] = 'O';
displayBoard();
}
checkForWinner();
if (checkForWinner() == true) {
System.out.println("\n" + human2.human2Name() + " has won the game!!!");
break;
}
if (board[0][0] != ' ' && board[0][1] != ' ' && board[0][2] != ' ' && board[1][0] != ' '
&& board[1][1] != ' ' && board[1][2] != ' ' && board[2][0] != ' ' && board[2][1] != ' '
&& board[2][2] != ' ') {
System.out.println("It's a draw!!!");
break;
}
}
}
public TicTacToe() {
board = new char[3][3];
}
public static void initializeBoard() {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
board[row][col] = ' ';
}
}
}
public static void displayBoard() {
System.out.println(" A B C");
System.out.println(" -------------");
for (int row = 0; row < 3; row++) {
System.out.print((row + 1) + " | ");
for (int col = 0; col < 3; col++) {
System.out.print(board[row][col] + " | ");
}
System.out.println();
System.out.println(" -------------");
}
}
public static boolean checkForWinner() {
if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][1] != ' ') {
return true;
}
if (board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][1] != ' ') {
return true;
}
if (board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][1] != ' ') {
return true;
}
if (board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[1][0] != ' ') {
return true;
}
if (board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[1][1] != ' ') {
return true;
}
if (board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[1][2] != ' ') {
return true;
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') {
return true;
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') {
return true;
}
return false;
}
public static boolean playAgain() {
System.out.println("\nWould you like to play again?");
keepPlaying = scan.nextLine();
yesOrNo = keepPlaying.toUpperCase().charAt(0);
if (yesOrNo == 'N') {
return false;
} else if (yesOrNo == 'Y') {
return true;
}
return false;
}
}

Related

Why does it not work when I input numOfTimes the code will stop running?(Name Generator)

I am a beginner in Java, and I have a question on why my code won't work. So basically when I set numOfTimes > 1 my program stops. Can anyone tell me why and how do I fix it? Thanks I will really appreciate if anyone helps me!
package CodingSets;
import java.util.*;
import java.util.*;
public class nameGenerator {
public static void rules() {
System.out.println("The rules arrr simple, give me the first
initial of your name, first intital of your middle name, and your last
initial");
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char firstInitial;
char middleInitial;
char lastInitial;
int numOfTimes = 0;
System.out.print("How many times do you want to to enter your or your friends initials?\t");
numOfTimes = scan.nextInt();
int j = 1;
while (j == numOfTimes) {
String name = "";
System.out.print("Enter your first initial : ");
firstInitial = scan.next().charAt(0);
scan = new Scanner(System.in);
System.out.print("Enter your middle initial : ");
middleInitial = scan.next().charAt(0);
scan = new Scanner(System.in);
System.out.print("Enter your last initial : ");
lastInitial = scan.next().charAt(0);
if(firstInitial == 'a' || firstInitial == 'A') {
name += "Captain";
} else if (firstInitial == 'b' || firstInitial == 'B') {
name += "Dirty";
} else if (firstInitial == 'c' || firstInitial == 'C') {
name += "Squidlips";
} else if (firstInitial == 'd' || firstInitial == 'D') {
name += "Bowman";
} else if (firstInitial == 'e' || firstInitial == 'E') {
name += "Buccaneer";
} else if (firstInitial == 'f' || firstInitial == 'F') {
name += "Two Toes";
} else if (firstInitial == 'g' || firstInitial == 'G') {
name += "Sharkbait";
} else if (firstInitial == 'h' || firstInitial == 'H') {
name += "Old";
} else if (firstInitial == 'i' || firstInitial == 'I') {
name += "Peg Leg";
} else if (firstInitial == 'j' || firstInitial == 'J') {
name += "Fluffbucket";
} else if (firstInitial == 'k' || firstInitial == 'K') {
name += "Scallywag";
} else if (firstInitial == 'l' || firstInitial == 'L') {
name += "Bucko";
} else if (firstInitial == 'm' || firstInitial == 'M') {
name += "Dead Man";
} else if (firstInitial == 'n' || firstInitial == 'N') {
name += "Matey";
} else if (firstInitial == 'o' || firstInitial == 'O') {
name += "Jolly";
} else if (firstInitial == 'p' || firstInitial == 'P') {
name += "Stinky";
} else if (firstInitial == 'q' || firstInitial == 'Q') {
name += "Bloody";
} else if (firstInitial == 'r' || firstInitial == 'R') {
name += "Miss";
} else if (firstInitial == 's' || firstInitial == 'S') {
name += "Mad";
} else if (firstInitial == 't' || firstInitial == 'T') {
name += "Red";
} else if (firstInitial == 'u' || firstInitial == 'U') {
name += "Lady";
} else if (firstInitial == 'v' || firstInitial == 'V') {
name += "Bretheren";
} else if (firstInitial == 'w' || firstInitial == 'W') {
name += "Rapscallion";
} else if (firstInitial == 'x' || firstInitial == 'X') {
name += "Landlubber";
} else if (firstInitial == 'y' || firstInitial == 'Y') {
name += "Wrench";
} else if (firstInitial == 'z' || firstInitial == 'Z') {
name += "Freebooter";
} else {
System.out.println("Bruh learn ur alphebet");
}
if(middleInitial == 'a' || middleInitial == 'A') {
name += " Creeper";
} else if (middleInitial == 'b' || middleInitial == 'B') {
name += " Jim";
} else if (middleInitial == 'c' || middleInitial == 'C') {
name += " Storm";
} else if (middleInitial == 'd' || middleInitial == 'D') {
name += " John";
} else if (middleInitial == 'e' || middleInitial == 'E') {
name += " George";
} else if (middleInitial == 'f' || middleInitial == 'F') {
name += " O'";
} else if (middleInitial == 'g' || middleInitial == 'G') {
name += " Rat";
} else if (middleInitial == 'h' || middleInitial == 'H') {
name += " Jack";
} else if (middleInitial == 'i' || middleInitial == 'I') {
name += " Legs";
} else if (middleInitial == 'j' || middleInitial == 'J') {
name += " Head";
} else if (middleInitial == 'k' || middleInitial == 'K') {
name += " Cackle";
} else if (middleInitial == 'l' || middleInitial == 'L') {
name += " Patch";
} else if (middleInitial == 'm' || middleInitial == 'M') {
name += " Bones";
} else if (middleInitial == 'n' || middleInitial == 'N') {
name += " Plank";
} else if (middleInitial == 'o' || middleInitial == 'O') {
name += " Greedy";
} else if (middleInitial == 'p' || middleInitial == 'P') {
name += " Sea";
} else if (middleInitial == 'q' || middleInitial == 'Q') {
name += " Mama";
} else if (middleInitial == 'r' || middleInitial == 'R') {
name += " Spike";
} else if (middleInitial == 's' || middleInitial == 'S') {
name += " Squiffy";
} else if (middleInitial == 't' || middleInitial == 'T') {
name += " Gold";
} else if (middleInitial == 'u' || middleInitial == 'U') {
name += " Yellow";
} else if (middleInitial == 'v' || middleInitial == 'V') {
name += " Felony";
} else if (middleInitial == 'w' || middleInitial == 'W') {
name += " Eddie";
} else if (middleInitial == 'x' || middleInitial == 'X') {
name += " Bay";
} else if (middleInitial == 'y' || middleInitial == 'Y') {
name += " Thomas";
} else if (middleInitial == 'z' || middleInitial == 'Z') {
name += " Spot";
} else {
System.out.println("Bruh learn ur alphebet");
}
if(lastInitial == 'a' || lastInitial == 'A') {
name += " From the West";
} else if (lastInitial == 'b' || lastInitial == 'B') {
name += " Byrd";
} else if (lastInitial == 'c' || lastInitial == 'C') {
name += " Jackson";
} else if (lastInitial == 'd' || lastInitial == 'D') {
name += " Sparrow";
} else if (lastInitial == 'e' || lastInitial == 'E') {
name += " Of the Coast";
} else if (lastInitial == 'f' || lastInitial == 'F') {
name += " Jones";
} else if (lastInitial == 'g' || lastInitial == 'G') {
name += " Ned Head";
} else if (lastInitial == 'h' || lastInitial == 'H') {
name += " Bart";
} else if (lastInitial == 'i' || lastInitial == 'I') {
name += " O'Fish";
} else if (lastInitial == 'j' || lastInitial == 'J') {
name += " Kidd";
} else if (lastInitial == 'k' || lastInitial == 'K') {
name += " O'Malley";
} else if (lastInitial == 'l' || lastInitial == 'L') {
name += " Barnacle";
} else if (lastInitial == 'm' || lastInitial == 'M') {
name += " HolyStone";
} else if (lastInitial == 'n' || lastInitial == 'N') {
name += " Hornswaggle";
} else if (lastInitial == 'o' || lastInitial == 'O') {
name += " McStinky";
} else if (lastInitial == 'p' || lastInitial == 'P') {
name += " Swashbuckler";
} else if (lastInitial == 'q' || lastInitial == 'Q') {
name += " Sea Wolf";
} else if (lastInitial == 'r' || lastInitial == 'R') {
name += " Beard";
} else if (lastInitial == 's' || lastInitial == 'S') {
name += " Chumbucket";
} else if (middleInitial == 't' || lastInitial == 'T') {
name += " Rivers";
} else if (middleInitial == 'u' || lastInitial == 'U') {
name += " Morgan";
} else if (lastInitial == 'v' || lastInitial == 'V') {
name += " Tuna Breath";
} else if (lastInitial == 'w' || lastInitial == 'W') {
name += " Three Gates";
} else if (lastInitial == 'x' || lastInitial == 'X') {
name += " Bailey";
} else if (lastInitial == 'y' || lastInitial == 'Y') {
name += " Of Atlantis";
} else if (lastInitial == 'z' || lastInitial == 'Z') {
name += " Of Dark Water";
} else {
System.out.println("Bruh learn ur alphebet");
}
System.out.println("Your pirate name is " + name);
j++;
}
}
}
The First Image Is The Code Where numOfTimes = 1, The Second Image Is The Not Working Code Where numOfTimes > 1
It doesn't work because the condition j == numOfTimes is false, therefore nothing in the loop actually is executed. Change it to j <= numOfTimes.

printing special characters from char

I'm writing a program converts letters in a phone number into their corresponding digits, and it's almost complete aside from some formatting:
import java.util.Scanner;
public class PhoneKeypad
{
public static void main (String [] args)
{
Scanner scan = new Scanner (System.in);
String number;
int currentChar;
char character;
int finalNumber;
int getNumber;
int start;
String end;
currentChar = 0;
do
{
System.out.print ("\nEnter a phone number with letters: ");
number = scan.nextLine();
number = number.toUpperCase();
while (currentChar < number.length())
{
character = number.charAt(currentChar);
finalNumber = getNumber(character);
System.out.print (finalNumber);
currentChar++;
}
System.out.print ("\n\nContinue? <y/n> ");
end = scan.next();
System.out.flush();
scan.nextLine();
currentChar = 0;
} while (!end.equalsIgnoreCase("n"));
}
public static int getNumber (char uppercaseLetter)
{
int resultChar = 0;
if (Character.isLetter(uppercaseLetter))
{
if (uppercaseLetter == 'A' || uppercaseLetter == 'B' || uppercaseLetter == 'C')
resultChar = 2;
else if (uppercaseLetter == 'D' || uppercaseLetter == 'E' || uppercaseLetter == 'F')
resultChar = 3;
else if (uppercaseLetter == 'G' || uppercaseLetter == 'H' || uppercaseLetter == 'I')
resultChar = 4;
else if (uppercaseLetter == 'J' || uppercaseLetter == 'K' || uppercaseLetter == 'L')
resultChar = 5;
else if (uppercaseLetter == 'M' || uppercaseLetter == 'N' || uppercaseLetter == 'O')
resultChar = 6;
else if (uppercaseLetter == 'P' || uppercaseLetter == 'Q' || uppercaseLetter == 'R' || uppercaseLetter == 'S')
resultChar = 7;
else if (uppercaseLetter == 'T' || uppercaseLetter == 'U' || uppercaseLetter == 'V')
resultChar = 8;
else if (uppercaseLetter == 'W' || uppercaseLetter == 'X' || uppercaseLetter == 'Y' || uppercaseLetter == 'Z')
resultChar = 9;
}
else if (Character.isDigit(uppercaseLetter))
{
resultChar = Character.getNumericValue(uppercaseLetter);
}
return resultChar;
}
}
The issue I'm having is that I need to print the phone number back out exactly as the user entered it, aside the the letters. If the user enters 1-800-flowers, the program needs to print it back out as 1-800-3569377. It's the same for any special characters - hyphens, spaces, parenthesis, etc.
And the simpler the solution, the better, as I haven't yet gotten to arrays or anything more complicated than methods.

Java - Tic Tac Toe - Why won't my "switch player" method work?

Why does my switchPlayer() method only work once? The method should work independently just fine, and when I put it inside my playGame() method after placeMark(), it works continuously, but that messes up my currentPlayer variable, which messes up other methods. I do not want to make a player object, so I assume I am just missing something simple, like where to place the switchPlayer() method.
My result goes like this:
currentplayer 1, place your mark.
[places mark, all good]
currentplayer 2, place your mark.
[places mark, all good]
currentplayer 2, place your mark. // problem line.. doesn't switch
[places mark, all good]
Relevant code for sub class:
public void playGame(){
assignNames();
assignMarks();
explainGame();
while(checkWin() == false && fullBoard() == false){
getNum();
placeMark();
printBoard();
}
gameResult();
}
public void placeMark(){
if(validNumber() == true){
if(num == 1){
board[0][0] = currentMark;
switchPlayer();
}
if(num == 2){
board[0][1] = currentMark;
switchPlayer();
}
if(num == 3){
board[0][2] = currentMark;
switchPlayer();
}
if(num == 4){
board[1][0] = currentMark;
switchPlayer();
}
if(num == 5){
board[1][1] = currentMark;
switchPlayer();
}
if(num == 6){
board[1][2] = currentMark;
switchPlayer();
}
if(num == 7){
board[2][0] = currentMark;
switchPlayer();
}
if(num == 8){
board[2][1] = currentMark;
switchPlayer();
}
if(num == 9){
board[2][2] = currentMark;
switchPlayer();
}
}
}
public void switchPlayer(){
if(currentMark == 'X')
currentMark = 'O';
else{
currentMark = 'X';
}
if(currentMark == 'O' && playerMark1 == 'O'){
currentPlayer = playerName1;
}
else{
currentPlayer = playerName2;
}
}
Relevant code for main class:
TicTacToe game = new TicTacToe();
game.playGame();
It seems as though I should just post the whole code... so here it is.
public class TicTacToe {
private static char[][] board;
Scanner input = new Scanner(System.in);
int num;
String entry;
String playerName1;
char playerMark1;
String playerName2;
char playerMark2;
char currentMark;
String currentPlayer;
public void playGame(){
assignNames();
assignMarks();
explainGame();
while(checkWin() == false && fullBoard() == false){
getNum();
placeMark();
printBoard();
}
gameResult();
}
public TicTacToe(){
board = new char[3][3];
clearBoard();
} // end TicTacToe()
public void clearBoard(){
for(int a = 0; a < 3; a++){
for(int b = 0; b < 3; b++){
board[a][b] = ' ';
}
}
}// end clearBoard()
public void printBoard(){
System.out.println("-------------");
for (int a = 0; a < 3; a++) {
System.out.print("| ");
for (int b = 0; b < 3; b++) {
System.out.print(board[a][b] + " | ");
}
System.out.println();
System.out.println("-------------");
}
}
public boolean fullBoard() {
boolean full = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == ' ') {
full = false;
}
}
}
return full;
}
public void explainGame(){
System.out.println("-------------");
System.out.println("| 1 | 2 | 3 |");
System.out.println("-------------");
System.out.println("| 4 | 5 | 6 |");
System.out.println("-------------");
System.out.println("| 7 | 8 | 9 |");
System.out.println("-------------");
System.out.println("The above board shows which numbers you must enter to place your mark in their respective locations.\n");
System.out.println("Randomly assigning player marks. . .");
System.out.println(playerName1+" will be Player "+playerMark1+", and "+playerName2+" will be Player "+playerMark2 + ".");
System.out.println("Player X will go first. Press any key to begin playing.");
entry = input.nextLine();
}
public void assignNames(){
System.out.println("Enter a name for Player 1.");
entry = input.nextLine();
playerName1 = entry;
System.out.println("Enter a name for Player 2.");
entry = input.nextLine();
playerName2 = entry;
}
public void assignMarks(){
Random rand = new Random();
int i = rand.nextInt(2);
if( i == 0 ){
playerMark1 = 'X';
playerMark2 = 'O';
}
else{
playerMark1 = 'O';
playerMark2 = 'X';
}
currentMark = 'X';
if(playerMark1 == 'X')
currentPlayer = playerName1;
else
currentPlayer = playerName2;
}
public void switchPlayer(){
if(currentMark == 'X')
currentMark = 'O';
else{
currentMark = 'X';
}
if(currentMark == 'O' && playerMark1 == 'O'){
currentPlayer = playerName1;
}
else{
currentPlayer = playerName2;
}
}
public int getNum(){
System.out.println(currentPlayer + ", place your mark (1-9).");
num = input.nextInt();
return num;
}
public boolean validSpot(int num){
boolean goodSpot = false;
if(num == 1 && board[0][0] == ' ')
goodSpot = true;
if(num == 2 && board[0][1] == ' ')
goodSpot = true;
if(num == 3 && board[0][2] == ' ')
goodSpot = true;
if(num == 4 && board[1][0] == ' ')
goodSpot = true;
if(num == 5 && board[1][1] == ' ')
goodSpot = true;
if(num == 6 && board[1][2] == ' ')
goodSpot = true;
if(num == 7 && board[2][0] == ' ')
goodSpot = true;
if(num == 8 && board[2][1] == ' ')
goodSpot = true;
if(num == 9 && board[2][2] == ' ')
goodSpot = true;
if(goodSpot == false){
System.out.println("Input error. Make sure the spot is not taken.");
}
return goodSpot;
}
public boolean validRange(int num){
boolean goodRange = false;
if(num >= 1 && num <= 9 ){
goodRange = true;
}
if(goodRange == false){
System.out.println("Input error. Make sure the number is between 1-9.");
}
return goodRange;
}
public boolean validNumber(){
return ((validRange(num) && validSpot(num)));
}
public void placeMark(){
if(validNumber() == true){
if(num == 1){
board[0][0] = currentMark;
switchPlayer();
}
if(num == 2){
board[0][1] = currentMark;
switchPlayer();
}
if(num == 3){
board[0][2] = currentMark;
switchPlayer();
}
if(num == 4){
board[1][0] = currentMark;
switchPlayer();
}
if(num == 5){
board[1][1] = currentMark;
switchPlayer();
}
if(num == 6){
board[1][2] = currentMark;
switchPlayer();
}
if(num == 7){
board[2][0] = currentMark;
switchPlayer();
}
if(num == 8){
board[2][1] = currentMark;
switchPlayer();
}
if(num == 9){
board[2][2] = currentMark;
switchPlayer();
}
}
}
public boolean checkSpot(char c1, char c2, char c3) {
return ((c1 != ' ') && (c1 == c2) && (c2 == c3));
}
public boolean checkWin() {
return (checkWinRows() || checkWinColumns() || checkWinDiagonals());
}
public boolean checkWinRows() {
for (int i = 0; i < 3; i++) {
if (checkSpot(board[i][0], board[i][1], board[i][2]) == true) {
return true;
}
}
return false;
}
public boolean checkWinColumns() {
for (int i = 0; i < 3; i++) {
if (checkSpot(board[0][i], board[1][i], board[2][i]) == true) {
return true;
}
}
return false;
}
public boolean checkWinDiagonals() {
return ((checkSpot(board[0][0], board[1][1], board[2][2]) == true) || (checkSpot(board[0][2], board[1][1], board[2][0]) == true));
}
public void gameResult(){
switchPlayer();
if(checkWin()){
System.out.println("\nGAME OVER -- " +currentPlayer+ " WINS! WOO HOO!");
}
else if(fullBoard()){
System.out.println("\nGAME OVER -- DRAW!");
}
}
} // end TicTacToe
Change
if(currentMark == 'O' && playerMark1 == 'O')
to
if(currentPlayer==playerName2)
Your code doesn't always work because it only changes to player one when both current mark = 0 and player1 mark = 0. This is, it only works when player one was assigned "O" in the beginning of the game.
Hope it helps.
The switch player code does nothing if both the current and player1 mark is X. Try:
if (currentMark == playerMark1){
currentPlayer = playerName1;
} else {
currentPlayer = playerName2;
}

Why is the line printed twice?

I created a simple Tic Tac Toe game. Everything runs perfect except that at the end of the game, when the program decides the outcome it prints the last line twice. For example if player 1 wins it prints:
Player 1 has won
Player 1 has won
I've tried to write input.nextLine(); after the user enters the integer but with no luck.
Thanks in advance.
Here is my code:
public static void main(String[] args) {
//The board
char board[][] = new char [3][3];
//Players' characters
char player1 = 'X';
char player2 = 'O';
char turn = 0;
int rowNumber;
int columnNumber;
int count = 0;
//Welcome mesage
System.out.println("This is a tic tac toe game! \n\n");
System.out.println("Player1 - X \nPlayer 2 - O \n\n");
Scanner input = new Scanner(System.in);
//We display the board
board(board);
while (checkWin(board, player1) && checkWin(board, player2)
&& fullBoard(board)) {
count = count % 2;
if (count == 0) {
System.out.println("Player 1 turn! \n");
turn = player1;
count++;
} else if (count == 1) {
System.out.println("Player 2 turn!");
turn = player2;
count++;
}
while (true) {
System.out.println("Enter a row(1-3):");
rowNumber = input.nextInt();
System.out.println("Enter a column(1-3):");
columnNumber = input.nextInt();
if (board[rowNumber - 1][columnNumber - 1] != 'X'
&& board[rowNumber - 1][columnNumber - 1] != 'O') {
board[rowNumber - 1][columnNumber - 1] = turn;
break;
} else {
System.out.println("Place already taken. Enter again!");
}
}
board(board);
checkWin(board, player1);
checkWin(board, player2);
fullBoard(board);
}
System.out.println("Just checking;");
}
public static void board(char board[][]) {
System.out.println(board[0][0] + " | " + board[0][1] + " | "
+ board[0][2]);
System.out.println(board[1][0] + " | " + board[1][1] + " | "
+ board[1][2]);
System.out.println(board[2][0] + " | " + board[2][1] + " | "
+ board[2][2]);
}
public static boolean checkWin(char board[][], char player) {
if (board[0][0] == player && board[0][1] == player && board[0][2] == player ||
board[1][0] == player && board[1][1] == player && board[1][2] == player ||
board[2][0] == player && board[2][1] == player && board[2][2] == player ||
board[0][0] == player && board[1][0] == player && board[2][0] == player ||
board[0][1] == player && board[1][1] == player && board[2][1] == player ||
board[0][2] == player && board[1][2] == player && board[2][2] == player ||
board[0][0] == player && board[1][1] == player && board[2][2] == player ||
board[2][0] == player && board[1][1] == player && board[0][2] == player){
if(player == 'X'){
System.out.println("Player 1 has won!");
}
else if(player == 'O'){
System.out.println("Player 2 has won!");
}
return false;
}else {
return true;
}
}
public static boolean fullBoard(char board[][]) {
if ((board[0][0] == 'X' || board[0][0] == 'O')
&& (board[0][1] == 'X' || board[0][1] == 'O')
&& (board[0][2] == 'X' || board[0][2] == 'O')
&& (board[1][0] == 'X' || board[1][0] == 'O')
&& (board[1][1] == 'X' || board[1][1] == 'O')
&& (board[1][2] == 'X' || board[1][2] == 'O')
&& (board[2][0] == 'X' || board[2][0] == 'O')
&& (board[2][1] == 'X' || board[2][1] == 'O')
&& (board[2][2] == 'X' || board[2][2] == 'O')){
System.out.println("It's a tie!");
return false;
}else {
return true;
}
}
}
Because you are calling method twice, once in loop condition and second inside loop.
while (checkWin(board, player1) && checkWin(board, player2)
&& fullBoard(board)) {
....
checkWin(board, player1);
checkWin(board, player2);
}
You don't need to call it inside loop, in condition it is sufficient.
Change it to following...
while (checkWin(board, player1) && checkWin(board, player2)
&& fullBoard(board)) {
....
// Remove method call from here.
}
This is due to your while loop here :
while (checkWin(board, player1) && checkWin(board, player2)
&& fullBoard(board)){
// stuff
// and then again...
checkWin(board, player1);
checkWin(board, player2);
fullBoard(board);
}

Not getting any output when trying to convert letters in a message to numbers

I am trying to make a program where the user inputs a message and the program converts it into numbers (eg a=1, b=2, etc). However, I don't get any output! Any help would be appreciated. My code is below.
import java.io.PrintStream;
import java.util.*;
public class AllRand25 {
public static void main(String[] args) {
int[] letterArray;
int[] wordArray;
int l = 0;
String messageWord;
char messageChar;
letterArray = new int[26];
int[] messageArray = new int[10];
int y = 0;
System.out.print("Enter your message");
Scanner scanner = new Scanner(System.in);
String message = scanner.nextLine();
while (scanner.hasNext()) {
messageWord = scanner.next(message);
l = messageWord.length();
Scanner charScan = new Scanner(messageWord);
for(int i = 0; i < messageWord.length(); i++){
messageChar = messageWord.charAt(i);
if (messageChar == 'a' || messageChar == 'A' || messageChar == 'A') {
messageArray[y] = 1;
} else if (messageChar == 'b' || messageChar == 'B') {
messageArray[y] = 2;
} else if (messageChar == 'c' || messageChar == 'C') {
messageArray[y] = 3;
} else if (messageChar == 'd' || messageChar == 'D') {
messageArray[y] = 4;
} else if (messageChar == 'e' || messageChar == 'E') {
messageArray[y] = 5;
} else if (messageChar == 'f' || messageChar == 'F') {
messageArray[y] = 6;
} else if (messageChar == 'g' || messageChar == 'G') {
messageArray[y] = 7;
} else if (messageChar == 'h' || messageChar == 'H') {
messageArray[y] = 8;
} else if (messageChar == 'i' || messageChar == 'I') {
messageArray[y] = 9;
} else if (messageChar == 'j' || messageChar == 'J') {
messageArray[y] = 10;
} else if (messageChar == 'k' || messageChar == 'K') {
messageArray[y] = 11;
} else if (messageChar == 'l' || messageChar == 'L') {
messageArray[y] = 12;
} else if (messageChar == 'm' || messageChar == 'M') {
messageArray[y] = 13;
} else if (messageChar == 'n' || messageChar == 'N') {
messageArray[y] = 14;
} else if (messageChar == 'o' || messageChar == 'O') {
messageArray[y] = 15;
} else if (messageChar == 'p' || messageChar == 'P') {
messageArray[y] = 16;
} else if (messageChar == 'q' || messageChar == 'Q') {
messageArray[y] = 17;
} else if (messageChar == 'r' || messageChar == 'R') {
messageArray[y] = 18;
} else if (messageChar == 's' || messageChar == 'S') {
messageArray[y] = 19;
} else if (messageChar == 't' || messageChar == 'T') {
messageArray[y] = 20;
} else if (messageChar == 'u' || messageChar == 'U') {
messageArray[y] = 21;
} else if (messageChar == 'v' || messageChar == 'V') {
messageArray[y] = 22;
} else if (messageChar == 'w' || messageChar == 'W') {
messageArray[y] = 23;
} else if (messageChar == 'x' || messageChar == 'X') {
messageArray[y] = 24;
} else if (messageChar == 'y' || messageChar == 'Y') {
messageArray[y] = 25;
} else if (messageChar == 'z' || messageChar == 'Z') {
messageArray[y] = 26;
}
else if (messageChar == ' ') {
messageArray[y] = 27;
}
y++;
}
}
for(int i = 0; i < messageArray.length; i++){
System.out.println(messageArray[i]);
}
}
}
You have many errors. Too many to point out, it's simpler if I show the correct code with comments:
public static void main(String[] args) throws Exception {
int[] wordArray;
String messageWord;
char messageChar;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your message: ");
messageWord = scanner.nextLine(); // read once before entering loop
while (!messageWord.equals("exit")) { // notice the correct condition
// these two variables should be reset for each word
int y = 0;
// and each messageArray should be of the right size
int[] messageArray = new int[messageWord.length()];
for (int i = 0; i < messageWord.length(); i++) {
messageChar = messageWord.charAt(i);
// *** insert all those if conditions here ***
y++;
}
// this goes inside the main loop
for (int j = 0; j < messageArray.length; j++)
System.out.print(messageArray[j] + " ");
// we have to ask for the next word inside the main loop
System.out.print("\nEnter your message: ");
messageWord = scanner.nextLine();
}
}
Notice that to exit the loop, it's better to have a special string that can be used as a condition - I'm using the word "exit" for this.
The (or at least 'an') error is in this line:
messageWord = scanner.next(message);
You're specifying message as argument. This seems to be wrong as next(String pattern) requires a regular expression that must match the next input sequence. Just call the no-arg version of this method!
For one thing, the while loop ends even before the for-loop that loops through the messageArray.
Right now the problem as I see it isn't that your code doesn't give results, it is that it doesn't ever end. It infinitely runs.
You are going to want to rewrite your entire code for this. I don't think that there are any quick fixes to make this work how you want.
Might I suggest looking into the StringBuilder class to help you this this project?
It is a way to use Strings as indexed Arrays instead of as one object. It could make what you are trying to do much easier.
The reason because you don't see anything is because you retrieved the input outside the outer while() loop, and completely forget to do anything useful with that.
Try to comment out this line
String message = scanner.nextLine();
and your program will run (but there still is an ArrayIndexOutOfBoundsException, so you need to fix that one too).

Categories

Resources