Assignment on TicTacToe
3 posters
IITDU Forum :: Academics (BIT) :: Semester 2 :: SE 206
Page 1 of 1
Assignment on TicTacToe
I have tried my own way to complete the assignment.
1.I take a [3][3] array.
2.I fixed two markers 'X' & 'O' for player 1 & 2.
3.Then I ask user to input & show him the game board.Which spot of the game board he selects,first I check that number is it in the game board? If it is there,I place his marker on that position otherwise ask him to input another number.Here is a problem.Because I ask the user for another input but in the meantime the playing option switched to next player.So I have to use switchPlayer() for two times if condition falls.
4.If a player fixes his spot the marker will be replaced with the position number.So if a player tries to
choose the spot again It will not find his desired position number because it is already replaced by a marker.So he can't input.
5.GameWinner() will check if there is same marker in a row,column or diagonal. i do this worker manually.I want an idea how can I do it more efficiently?
6.3,4 & 5 runs for 9 valid play,if it finds a winner breaks.otherwise after 9 valid plays it shows draw.
Do you have any suggestion to make the code more efficient?
1.I take a [3][3] array.
2.I fixed two markers 'X' & 'O' for player 1 & 2.
3.Then I ask user to input & show him the game board.Which spot of the game board he selects,first I check that number is it in the game board? If it is there,I place his marker on that position otherwise ask him to input another number.Here is a problem.Because I ask the user for another input but in the meantime the playing option switched to next player.So I have to use switchPlayer() for two times if condition falls.
4.If a player fixes his spot the marker will be replaced with the position number.So if a player tries to
choose the spot again It will not find his desired position number because it is already replaced by a marker.So he can't input.
5.GameWinner() will check if there is same marker in a row,column or diagonal. i do this worker manually.I want an idea how can I do it more efficiently?
6.3,4 & 5 runs for 9 valid play,if it finds a winner breaks.otherwise after 9 valid plays it shows draw.
Do you have any suggestion to make the code more efficient?
- Code:
public class tMain {
public static void main(String[] args) {
ticTacToe game = new ticTacToe();
int continuePlaying=1;
while (continuePlaying==1) {
game.board();
game.drawBoard();
int player;
int t=0;
while ( game.getPlays() < 9) {
int pick = 1;
while (pick!=0) {
if (game.getCurrentPlayer() == 1)
player = game.getPlayer1();
else
player = game.getPlayer2();
System.out.print("It is Player" + player+ "'s turn. Pick a spot: ");
String square = game.Input();
if (square.length()==1&& Character.isDigit(square.toCharArray()[0])) {
pick = Integer.parseInt(square);
pick = game.placeMarker(pick);
}
if (pick == 0) {
System.out.println("Spot can not be selected. Retry");
game.switchPlayers();
}
game.drawBoard();
game.switchPlayers();
t=game.winner();
if(t==1)
break;
}
if(t==1)
break;
}
t=game.winner();
if (t==1) {
player = game.getCurrentPlayer();
if (player == 1)
System.out.println("Game Over: **Player2** WINS!");
if (player == 2)
System.out.println("Game Over: **Player1** WINS!");
} else {
System.out.println("Game Over: Draw");
}
System.out.println();
System.out.print("Play again? (Y/N): ");
String choice = game.Input();
if (choice=="N") {
continuePlaying=2;
}
}
}
}
import java.util.*;
public class ticTacToe{
char[][] board = new char[3][3];
int player1;
int player2;
int currentPlayer=1;
int plays=0;
char marker1 ='X';
char marker2='O';
protected void board() {
int k=0;
for (int i = 0; i < 3; i++) {
for (int j= 0; j<3; j++) {
board[i][j] =Character.forDigit(++k,10);
}
}
currentPlayer=1;
plays=0;
}
void drawBoard() {
System.out.println("Game board:");
for (int i = 0; i < 3; i++) {
for (int j= 0;j< 3;j++) {
System.out.print("[" +board[i][j] + "]");
}
System.out.println();
}
}
void switchPlayers() {
if (getCurrentPlayer()==1) {
setCurrentPlayer(2);
} else {
setCurrentPlayer(1);
}
setPlays(getPlays()+1);
}
String Input() {
String input="";
Scanner sc=new Scanner(System.in);
input=sc.next();
return input;
}
int placeMarker(int play) {
for (int i = 0; i < 3; i++) {
for (int j= 0; j< 3; j++) {
if (board[i][j]==Character.forDigit(play,10)) {
if(getCurrentPlayer()==1)
board[i][j]=marker1;
else
board[i][j]=marker2;
return (play+1);
}
}
}
return 0;
}
int winner() {
char current = ' ';
current =board[0][0];
if (Character.isLetter(current) && board[0][1] == current && board[0][2] == current) {
return 1;
}
current =board[1][0];
if
(Character.isLetter(current) && board[1][1] == current && board[1][2] == current) {
return 1;
}
current =board[2][0];
if (Character.isLetter(current) && board[2][1] == current && board[2][2] == current) {
return 1;
}
current =board[0][0];
if (Character.isLetter(current) && board[1][0] == current && board[2][0] == current) {
return 1;
}
current =board[0][1];
if (Character.isLetter(current) && board[1][1] == current && board[2][1] == current) {
return 1;
}
current =board[0][2];
if (Character.isLetter(current) && board[1][2] == current && board[2][2] == current) {
return 1;
}
current =board[0][0];
if (Character.isLetter(current) && board[1][1] == current && board[2][2] == current) {
return 1;
}
current =board[0][2];
if (Character.isLetter(current) && board[1][1] == current && board[2][0] == current) {
return 1;
}
else
return 0;
}
int getCurrentPlayer() {
return currentPlayer;
}
void setCurrentPlayer(int currentPlayer) {
this.currentPlayer = currentPlayer;
}
int getPlays() {
return plays;
}
void setPlays(int plays) {
this.plays = plays;
}
int getPlayer1() {
int player1=1;
return player1;
}
void setPlayer1(int player1) {
this.player1 = player1;
}
int getPlayer2() {
int player2=2;
return player2;
}
void setPlayer2(int player2) {
this.player2 = player2;
}
}
BIT0322-Hira- Alpha Release
- Course(s) :
- BIT
Blood Group : O+
Posts : 24
Points : 33
Re: Assignment on TicTacToe
No doubt it’s a good work. One think just immediately come to my mind that you didn’t follow coding convention. I didn’t check your logic. The problem I have found while my first scans are -
Change your class name. By convention and coding standard, class
name should start with uppercase. And consequent words are also should be uppercase. Example - TicTacToe
2. Class variable should be private.
3. The methods those are not visible outside the class, should be private.
4. String Input(), method is not understandable. same things true for the for winner(). You could write it getWinner().
5. What does the method board() do? I didn't get it. so, change it.
Suggestion:
1. To make code more readable, follow coding convention strictly.
2. Method name should be a verb and variable, and class name should be a noun.
3. study on java coding standard and coding convention .
I didn't check your logic. I will do it whenever I get some free time.
All the best and happy coding. [You must be registered and logged in to see this image.]
Change your class name. By convention and coding standard, class
name should start with uppercase. And consequent words are also should be uppercase. Example - TicTacToe
2. Class variable should be private.
3. The methods those are not visible outside the class, should be private.
4. String Input(), method is not understandable. same things true for the for winner(). You could write it getWinner().
5. What does the method board() do? I didn't get it. so, change it.
Suggestion:
1. To make code more readable, follow coding convention strictly.
2. Method name should be a verb and variable, and class name should be a noun.
3. study on java coding standard and coding convention .
I didn't check your logic. I will do it whenever I get some free time.
All the best and happy coding. [You must be registered and logged in to see this image.]
Re: Assignment on TicTacToe
Good
work, now do it again in swing. User doesn't like to play a game in the console. So make a good user interface with swing. Study a bit on Java's swing. I think you will enjoy it. Take a week; I will knock you after a week to see your progress. All the best,
work, now do it again in swing. User doesn't like to play a game in the console. So make a good user interface with swing. Study a bit on Java's swing. I think you will enjoy it. Take a week; I will knock you after a week to see your progress. All the best,
Re: Assignment on TicTacToe
Just in case you don't know what is Swing, it is something that can be used to create GUI using Java
There are many tutorials available.
One of my favorites is this one-
[You must be registered and logged in to see this link.]
There are many tutorials available.
One of my favorites is this one-
[You must be registered and logged in to see this link.]
Similar topics
» What is the new assignment?
» Psychology assignment
» About assignment of group 4
» reading assignment of c++
» Vehicle Assignment
» Psychology assignment
» About assignment of group 4
» reading assignment of c++
» Vehicle Assignment
IITDU Forum :: Academics (BIT) :: Semester 2 :: SE 206
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford