tic tac toe 11-12-2013, 01:51 AM
#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!!
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
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
(This post was last modified: 11-12-2013, 01:51 AM by LordPankake.)
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)


![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)