Login Register


tic tac toe filter_list
Author
Message
tic tac toe #1
few days ago i saw @Ex094 working on making a tic tac toe game in c i found it interesting and it will be a good challenge for me so i started working on that project !!
as every1 helped me on my first project rock scissors paper game and gave me feedbacks to make it better i wish you could do the same here !!

first i'll share with you my code i'm stuck somewhere couldn't figure out a way to do it so you guys could start by helping me solving this problem then giving me advices on how to make my code better!!

Spoiler:
Code:
package tictactoe; import java.util.Random; import java.util.Scanner; public class TicTacToe { public static void viewBoard(char board[]){ System.out.println("|"+board[0]+"|"+board[1]+"|"+board[2]+"|"); System.out.println("|"+board[3]+"|"+board[4]+"|"+board[5]+"|"); System.out.println("|"+board[6]+"|"+board[7]+"|"+board[8]+"|"); } public static int whoStart(){ int turn; int rand=new Random().nextInt(2); if(rand==1){return turn=1;} return turn=2; } public static int makeMove(char board[],int turn,int box){ if(turn%2==0){board[box]='o';} else{board[box]='x'; } return box; } public static boolean isValidMove(char board[],int box){ if ((board[box]=='x')||(board[box]=='o')){return true; } return false; } public static boolean checkWin(char board[]){ //first horizontal line if(((board[0]==board[1])&&(board[1]==board[2])&&(board[2]=='o'))|| ((board[0]==board[1])&&(board[1]==board[2])&&(board[2]=='x'))|| //seconde horizontal line ((board[3]==board[4])&&(board[4]==board[5])&&(board[5]=='o'))|| ((board[3]==board[4])&&(board[4]==board[5])&&(board[5]=='x'))|| //third horizontal line ((board[6]==board[7])&&(board[7]==board[8])&&(board[8]=='o'))|| ((board[6]==board[7])&&(board[7]==board[8])&&(board[8]=='x'))|| //first vertical line ((board[0]==board[3])&&(board[3]==board[6])&&(board[6]=='o'))|| ((board[0]==board[3])&&(board[3]==board[6])&&(board[6]=='x'))|| //seconde vertical line ((board[1]==board[4])&&(board[4]==board[7])&&(board[7]=='o'))|| ((board[1]==board[4])&&(board[4]==board[7])&&(board[7]=='x'))|| //third vertical line ((board[2]==board[5])&&(board[5]==board[8])&&(board[8]=='o'))|| ((board[2]==board[5])&&(board[5]==board[8])&&(board[8]=='x'))|| //first diagonal line ((board[0]==board[4])&&(board[4]==board[8])&&(board[8]=='o'))|| ((board[0]==board[4])&&(board[4]==board[8])&&(board[8]=='x'))|| //seconde diagonal line ((board[6]==board[4])&&(board[4]==board[2])&&(board[2]=='o'))) {return true;} return false; } public static void main(String[] args) { Scanner in=new Scanner(System.in); char board[]={'0','1','2','3','4','5','6','7','8'}; System.out.println("enter player 1 name"); String player1Name=in.nextLine(); System.out.println("enter player 2 name"); String player2Name=in.nextLine(); viewBoard(board); System.out.println(whoStart()); int turn=whoStart();int counter=0; do{ System.out.println("choose box number"); int box=in.nextInt(); while(isValidMove(board,box)){ System.out.println("choose another box"); box=in.nextInt();} makeMove(board,turn,box); viewBoard(board); turn++; counter++; } while ((counter<9)&&(checkWin(board)==false)); }


ok so as you can see in my main it keeps asking to choose a box until the counter is equal or bigger then 9 or we have a winner!!
what i want to do is after the do while is over if we have a winner i wanted to say the winner is player 1 if player 1 won or the winner is player 2 if player 2 won and finally it's a tie if no1 won !!
can u give me ideas on how to do it ?
thx in advance for the help Smile
(This post was last modified: 11-12-2013, 01:51 AM by LordPankake.)
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: tic tac toe #2
I think the CheckWin part can be done in a more clear way using Java, Do some searches on Google and I'm sure you'll find it.
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: tic tac toe #3
i'll work on this later but first i need a little help to be able to finish my code !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: tic tac toe #4
Quote:what i want to do is after the do while is over if we have a winner i wanted to say the winner is player 1 if player 1 won or the winner is player 2 if player 2 won and finally it's a tie if no1 won !!
can u give me ideas on how to do it ?

Instead of returning a boolean let the checkWin return who won.
Look into Enums: http://docs.oracle.com/javase/tutorial/j.../enum.html
Then create an enum type that has the values: PLAYER1, PLAYER2, NO_WINNER
Then you will say something like:

Code:
while ((counter < 9) && (checkWin(board) == NO_WINNER)); if (counter >= 9) { //tie } else if (checkWin(board) == PLAYER1) { //player 1 is winner } else { //player 2 is winner }
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply







Users browsing this thread: 1 Guest(s)