🔒 Closed Tic tac toe ^^

Status
Not open for further replies.

Princess Serrano

Honorary Poster
import java.util.Scanner ;
import java.util.Random ;

class XOGame {

// REM: New printing format
static void printLine ( String inStream ) {
System.out.println ( inStream ) ;
}
static void print ( String inStream ) {
System.out.print ( inStream ) ;
}
static void newLine ( ) {
System.out.println ( ) ;
}
static void space ( int blank ) {
String lank = "" ;
for ( int a = 0 ; a < blank ; a ++ ) {
lank += " " ;
}
print ( lank ) ;
}
static void timer ( int time ) {
try {
Thread.sleep ( time ) ;
}
catch ( Exception e ) {

}
}

public static void main ( String [ ] args ) {

Scanner intel = new Scanner ( System.in ) ;

// REM: Constant variable
boolean tryAgain = false ;
int myScore = 0 ;
int enemyScore = 0 ;
int round = 1 ;
String myCharacter = "" ;
String myEnemy = "" ;
int whosturn = 0 ;
int preturn = 1 ;

// REM: Introduction
newLine ( ) ;
space ( 3 ) ;
printLine ( "Welcome to the X-O Game Program!" ) ;
space ( 3 ) ;
printLine ( "Made by Princess Serrano. | October 30-31 , 2017" ) ;
newLine ( ) ;

// REM: Choose your character.
while ( true ) {
space ( 3 ) ;
print ( "Choose your character ( X or O ): " ) ;
myCharacter = intel.next ( ) ;
if ( myCharacter.equalsIgnoreCase ( "X" ) ) {
myCharacter = "X" ;
myEnemy = "O" ;
newLine ( ) ;
space ( 4 ) ;
printLine ( "You choose " + myCharacter + "!" ) ;
break ;
}
else if ( myCharacter.equalsIgnoreCase ( "O" ) ) {
myCharacter = "O" ;
myEnemy = "X" ;
newLine ( ) ;
space ( 4 ) ;
printLine ( "You choose " + myCharacter + "!" ) ;
break ;
}
else {
space ( 4 ) ;
printLine ( "Unknown character." ) ;
continue ;
}
}


// REM: Repeat the game when there is winner
while ( !tryAgain ) {

// REM: Check wether who's turn
whosturn = new Random ( ) . nextInt ( 2 ) ;

// REM: Setting variables
String [ ] locationValues = { "1" , "2" , "3" , "4" , "5", "6" , "7" , "8" , "9" } ;
String xlineBoard = "-------------" ;
String champion = "" ;
String boxnumbers = "" ;
int myMove = 0 ;
int turns = 0 ;
int cap = 0 ;
boolean winner = false ;
boolean draw = false ;
boolean endTurn = false ;

// REM: Boardgame initial display
newLine ( ) ;
space ( 4 ) ;
printLine ( "-> Round " + round + " <-" ) ;
round ++ ;
String ylineBoard_1 = "| " + locationValues [ 0 ] + " | " + locationValues [ 1 ] + " | " + locationValues [ 2 ] + " |" ;
String ylineBoard_2 = "| " + locationValues [ 3 ] + " | " + locationValues [ 4 ] + " | " + locationValues [ 5 ] + " |" ;
String ylineBoard_3 = "| " + locationValues [ 6 ] + " | " + locationValues [ 7 ] + " | " + locationValues [ 8 ] + " |" ;

space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_1 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_2 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_3 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;

// REM: Ask to hide numbers
newLine ( ) ;
while ( true ) {
space ( 4 ) ;
print ( "Do you want to hide box numbers? (Yes or No): " ) ;
boxnumbers = intel.next ( ) ;
if ( boxnumbers . equalsIgnoreCase ( "Yes" ) ) {
for ( int a = 0 ; a < locationValues.length ; a ++ ) {
locationValues [ a ] = " " ;
}
// REM: Displaying new preview
newLine ( ) ;
ylineBoard_1 = "| " + locationValues [ 0 ] + " | " + locationValues [ 1 ] + " | " + locationValues [ 2 ] + " |" ;
ylineBoard_2 = "| " + locationValues [ 3 ] + " | " + locationValues [ 4 ] + " | " + locationValues [ 5 ] + " |" ;
ylineBoard_3 = "| " + locationValues [ 6 ] + " | " + locationValues [ 7 ] + " | " + locationValues [ 8 ] + " |" ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_1 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_2 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_3 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
if ( whosturn == 0 ) {
space ( 4 ) ;
printLine ( "It's your turn to move!" ) ;
}
else {
space ( 4 ) ;
printLine ( "It's enemy turn to move!" ) ;
}
newLine ( ) ;
break ;
}
else if ( boxnumbers . equalsIgnoreCase ( "No" ) ) {
// REM: Displaying new preview
newLine ( ) ;
ylineBoard_1 = "| " + locationValues [ 0 ] + " | " + locationValues [ 1 ] + " | " + locationValues [ 2 ] + " |" ;
ylineBoard_2 = "| " + locationValues [ 3 ] + " | " + locationValues [ 4 ] + " | " + locationValues [ 5 ] + " |" ;
ylineBoard_3 = "| " + locationValues [ 6 ] + " | " + locationValues [ 7 ] + " | " + locationValues [ 8 ] + " |" ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_1 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_2 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_3 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
if ( whosturn == 0 ) {
space ( 4 ) ;
printLine ( "It's your turn to move!" ) ;
}
else {
space ( 4 ) ;
printLine ( "It's enemy turn to move!" ) ;
}
newLine ( ) ;
break ;
}
else {
space ( 4 ) ;
printLine ( "Please choose between yes or no." ) ;
continue ;
}
}

while ( !winner ) {
// REM: Choose location myturn
if ( whosturn == 0 ) {
cap = 8 ;
if ( preturn == 1 ) {
endTurn = false ;
cap = 7 ;
}
if ( !endTurn ) {
while ( true ) {
space ( 4 ) ;
print ( "Move: " ) ;
if ( !intel.hasNextInt ( ) ) {
space ( 4 ) ;
printLine ( "Input an integer number from ( 1 - 9 )" ) ;
intel.next ( ) ;
continue ;
}
else {
myMove = intel.nextInt ( ) ;
if ( myMove < 1 || myMove > 9 ) {
space ( 4 ) ;
printLine ( "Out of range. ( 1 - 9 only )" ) ;
continue ;
}
else {
break ;
}
}
}
}


// REM: Replace the array
if ( locationValues [ myMove - 1 ].equals ( "X" ) || locationValues [ myMove - 1 ] .equals ( "O" ) ) {
space ( 4 ) ;
printLine ( myMove + " has already been taken." ) ;
}
else {
locationValues [ myMove - 1 ] = myCharacter ;
turns ++ ;
if ( turns <= cap ) {
endTurn = true ;
whosturn = 1 ;
preturn ++ ;
}
}

// REM: Boardgame post-display for mymove
if ( !winner ) {
newLine ( ) ;
ylineBoard_1 = "| " + locationValues [ 0 ] + " | " + locationValues [ 1 ] + " | " + locationValues [ 2 ] + " |" ;
ylineBoard_2 = "| " + locationValues [ 3 ] + " | " + locationValues [ 4 ] + " | " + locationValues [ 5 ] + " |" ;
ylineBoard_3 = "| " + locationValues [ 6 ] + " | " + locationValues [ 7 ] + " | " + locationValues [ 8 ] + " |" ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_1 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_2 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_3 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
newLine ( ) ;
}

// REM: Check if winner for my character
if ( locationValues [ 0 ].equals ( myCharacter ) && locationValues [ 1 ].equals ( myCharacter ) && locationValues [ 2 ].equals ( myCharacter ) ) {
champion = myCharacter ;
winner = true ;
endTurn = false ;
myScore ++ ;
}

else if ( locationValues [ 3 ].equals ( myCharacter ) && locationValues [ 4 ].equals ( myCharacter ) && locationValues [ 5 ].equals ( myCharacter ) ) {
champion = myCharacter ;
winner = true ;
endTurn = false ;
myScore ++ ;
}

else if ( locationValues [ 6 ].equals ( myCharacter ) && locationValues [ 7 ].equals ( myCharacter ) && locationValues [ 8 ].equals ( myCharacter ) ) {
champion = myCharacter ;
winner = true ;
endTurn = false ;
myScore ++ ;
}

else if ( locationValues [ 0 ].equals ( myCharacter ) && locationValues [ 3 ].equals ( myCharacter ) && locationValues [ 6 ].equals ( myCharacter ) ) {
champion = myCharacter ;
winner = true ;
endTurn = false ;
myScore ++ ;
}

else if ( locationValues [ 1 ].equals ( myCharacter ) && locationValues [ 4 ].equals ( myCharacter ) && locationValues [ 7 ].equals ( myCharacter ) ) {
champion = myCharacter ;
winner = true ;
endTurn = false ;
myScore ++ ;
}

else if ( locationValues [ 2 ].equals ( myCharacter ) && locationValues [ 5 ].equals ( myCharacter ) && locationValues [ 8 ].equals ( myCharacter ) ) {
champion = myCharacter ;
winner = true ;
endTurn = false ;
myScore ++ ;
}

else if ( locationValues [ 0 ].equals ( myCharacter ) && locationValues [ 4 ].equals ( myCharacter ) && locationValues [ 8 ].equals ( myCharacter ) ) {
champion = myCharacter ;
winner = true ;
endTurn = false ;
myScore ++ ;
}

else if ( locationValues [ 2 ].equals ( myCharacter ) && locationValues [ 4 ].equals ( myCharacter ) && locationValues [ 6 ].equals ( myCharacter ) ) {
champion = myCharacter ;
winner = true ;
endTurn = false ;
myScore ++ ;
}

else if ( turns == 9 && !winner ) {
draw = true ;
}
}

// REM: Check if enemies turn
if ( whosturn == 1 ) {
cap = 8 ;
if ( preturn == 1 ) {
cap = 7 ;
endTurn = true ;
}
if ( endTurn ) {
while ( true ) {
int temp = new Random ( ) . nextInt ( 9 ) ;
if ( !locationValues [ temp ].equals ( myCharacter ) && !locationValues [ temp ].equals ( myEnemy ) ) {
space ( 4 ) ;
timer ( 1500 ) ;
printLine ( "Enemy move: " + ( temp + 1 ) ) ;
locationValues [ temp ] = myEnemy ;
if ( turns <= cap ) {
endTurn = false ;
}
turns ++ ;
timer ( 1000 ) ;
whosturn = 0 ;
preturn ++ ;
break ;
}
}

}

if ( !winner ) {
// REM: Boardgame post-display for enemy move
newLine ( ) ;
ylineBoard_1 = "| " + locationValues [ 0 ] + " | " + locationValues [ 1 ] + " | " + locationValues [ 2 ] + " |" ;
ylineBoard_2 = "| " + locationValues [ 3 ] + " | " + locationValues [ 4 ] + " | " + locationValues [ 5 ] + " |" ;
ylineBoard_3 = "| " + locationValues [ 6 ] + " | " + locationValues [ 7 ] + " | " + locationValues [ 8 ] + " |" ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_1 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_2 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
space ( 4 ) ;
printLine ( ylineBoard_3 ) ;
space ( 4 ) ;
printLine ( xlineBoard ) ;
newLine ( ) ;
}

// REM: Check if winner for enemy

if ( locationValues [ 0 ].equals ( myEnemy ) && locationValues [ 1 ].equals ( myEnemy ) && locationValues [ 2 ].equals ( myEnemy ) ) {
champion = myEnemy ;
winner = true ;
endTurn = true ;
enemyScore ++ ;
}

else if ( locationValues [ 3 ].equals ( myEnemy ) && locationValues [ 4 ].equals ( myEnemy ) && locationValues [ 5 ].equals ( myEnemy ) ) {
champion = myEnemy ;
winner = true ;
endTurn = true ;
enemyScore ++ ;
}

else if ( locationValues [ 6 ].equals ( myEnemy ) && locationValues [ 7 ].equals ( myEnemy ) && locationValues [ 8 ].equals ( myEnemy ) ) {
champion = myEnemy ;
winner = true ;
endTurn = true ;
enemyScore ++ ;
}

else if ( locationValues [ 0 ].equals ( myEnemy ) && locationValues [ 3 ].equals ( myEnemy ) && locationValues [ 6 ].equals ( myEnemy ) ) {
champion = myEnemy ;
winner = true ;
endTurn = true ;
enemyScore ++ ;
}

else if ( locationValues [ 1 ].equals ( myEnemy ) && locationValues [ 4 ].equals ( myEnemy ) && locationValues [ 7 ].equals ( myEnemy ) ) {
champion = myEnemy ;
winner = true ;
endTurn = true ;
enemyScore ++ ;
}

else if ( locationValues [ 2 ].equals ( myEnemy ) && locationValues [ 5 ].equals ( myEnemy ) && locationValues [ 8 ].equals ( myEnemy ) ) {
champion = myEnemy ;
winner = true ;
endTurn = true ;
enemyScore ++ ;
}

else if ( locationValues [ 0 ].equals ( myEnemy ) && locationValues [ 4 ].equals ( myEnemy ) && locationValues [ 8 ].equals ( myEnemy ) ) {
champion = myEnemy ;
winner = true ;
endTurn = true ;
enemyScore ++ ;
}

else if ( locationValues [ 2 ].equals ( myEnemy ) && locationValues [ 4 ].equals ( myEnemy ) && locationValues [ 6 ].equals ( myEnemy ) ) {
champion = myEnemy ;
winner = true ;
endTurn = true ;
enemyScore ++ ;
}
else if ( turns == 9 && !winner ) {
draw = true ;
}
}

// REM: Conclusion & decision
if ( winner ) {
space ( 4 ) ;
printLine ( "The winner is " + champion ) ;
space ( 4 ) ;
printLine ( "My score: " + myScore +
" | Enemy score: " + enemyScore ) ;
winner = true ;
timer ( 2500 ) ;
preturn = 1 ;
}
else if ( draw ) {
space ( 4 ) ;
printLine ( "The fight is draw" ) ;
timer ( 2500 ) ;
winner = true ;
preturn = 1 ;
}

}

}

}
}
 
not bad.
ngayon, gawa ka ng VERSION 2 and in this version you eliminate DUPLICATE CODES.
Either you create REUSABLE METHODS or create CLASSES that will handle these duplicate codes.

Isang obvious candidate ay yung "displaying and updating of the board" (lines 106, 137, 168, 254, 366). You can either put that in a reusable method or create a class just for it.

And another good candidate to put on its own method or class ay yung pag-che-check kung "whose turn to play".

Also candidate ay yung pagche-check for the winner.

WARNING: do this slowly and small chunks. And test as you go.

Good job and keep coding (nasa level 3 ka na. Level 5, puwede ka na isali sa team ko as Jr.
Puwede na ba sa iyo ang $3,000 a month? ;)
 
Status
Not open for further replies.

About this Thread

  • 2
    Replies
  • 579
    Views
  • 3
    Participants
Last reply from:
codyscott

Trending Topics

Online now

Members online
1,059
Guests online
1,021
Total visitors
2,080

Forum statistics

Threads
2,274,732
Posts
28,957,871
Members
1,233,410
Latest member
glennmart0525
Back
Top