How do you alternate between 2 human players?
6 12016 - int i;
-
while((i <= 9) && (!win)) // 9 squares, so 9 inputs.
-
{
-
System.out.println("Player " + (i%2) + "'s turn");
-
win = GetInputAndUpdate(((i%2)==1));
-
i++;
-
}
-
if(win)
-
{
-
System.out.println("Player " + ((i-1)%2) + " wins!");
-
}
-
Effectively, you are passing a boolean value that is true if it's player 1's turn to input data. Also, win is a boolean value that is returned if a player has one the game.
If you have an N player game, then do (i % N).
I have a boolean method named makeMove that tries to make a move and returns true if the move is valid and false if it isn't.
I also have a public String promptMove() method that prompts a player to move a piece on the board. -
public boolean makeMove( String move ){
-
-
-
for(int row=0; row<3; row++){
-
-
-
for(int col=0; col<3; col++){
-
-
-
if(board[row][col]=="")
-
move.equals(board[row][col]);
-
-
canvas.repaint(board, row, col);
-
-
return true;
-
-
}
-
}
-
-
-
return false;
-
}
-
-
public String promptMove(){
-
-
while(!gameOver()){
-
-
currentPlayer=playerOne;
-
-
if(currentPlayer.makeMove(String move))
-
-
currentPlayer=playerTwo;
-
-
if(currentPlayer.makeMove(String move))
-
currentPlayer=playerOne;
-
-
if(currentPlayer.makeMove(String move))
-
currentPlayer=playerTwo;
-
-
if(currentPlayer.makeMove(String move))
-
currentPlayer=playerOne;
-
-
if(currentPlayer.makeMove(String move))
-
currentPlayer=playerTwo;
-
-
if(currentPlayer.makeMove(String move))
-
currentPlayer=playerOne;
-
-
if(currentPlayer.makeMove(String move))
-
currentPlayer=playerTwo;
-
-
if(currentPlayer.makeMove(String move))
-
currentPlayer=playerOne;
-
-
-
-
}
-
-
-
}
-
How do I set up a for loop in the promptMove method that will loop between playerOne which is "X" and playerTwo which is "O"?
I've finished all the code but I can't get the "X" and "O"s to show up on the board or to switch between human players. -
package csc216.project2;
-
-
import javax.swing.JOptionPane;
-
-
-
-
-
public class TicTacToe implements GameBoard{
-
-
private GameCanvas canvas;
-
-
private String [] [] board;
-
-
private String currentPlayer;
-
-
private String playerOne="X";
-
-
private String playerTwo="O";
-
-
private int row;
-
-
private int col;
-
-
-
-
public TicTacToe(){
-
-
canvas=new GameCanvas();
-
-
currentPlayer=null;
-
-
board=new String[3][3];
-
-
row=3;
-
-
col=3;
-
-
-
}
-
-
/**
-
*Draws current state of the board
-
*/
-
-
public void displayBoard(){
-
-
canvas.repaint(board, row, col);
-
-
-
-
-
}
-
-
/**
-
*Query whether the game is completed
-
*@return TRUE if game is over, FALSE otherwise
-
*/
-
-
public boolean gameOver(){
-
-
for(int row=0; row<3; row++ )
-
for(int col=0; col<3; col++)
-
-
-
if(board[0][col]=="X" || board[0][col]=="O")
-
return true;
-
-
else if(board[1][col]=="X" || board[1][col]=="O")
-
return true;
-
-
else if(board[2][col]=="X" || board[2][col]=="O")
-
return true;
-
-
else if(board[row][0]=="X" || board[row][0]=="O")
-
return true;
-
-
else if(board[row][1]=="X" || board[row][1]=="O")
-
return true;
-
-
else if(board[row][2]=="X" || board[row][2]=="O")
-
return true;
-
-
else if(board[0][0]=="X" && board[1][1]=="X" && board[2][2]=="X")
-
return true;
-
-
else if(board[0][0]=="O" && board[1][1]=="O" && board[2][2]=="O")
-
return true;
-
-
else if(board[2][0]=="X" && board[1][1]=="X" && board[0][2]=="X")
-
return true;
-
-
else if(board[2][0]=="O" && board[1][1]=="O" && board[0][2]=="O")
-
return true;
-
-
else if(board[row][col]!="")
-
return true;
-
-
-
-
return false;
-
-
-
-
}
-
-
/**
-
*Initialize the board to a starting state
-
*/
-
-
public void initBoard(){
-
-
for(int row=0; row<3; row++)
-
-
for(int col=0; col<3; col++)
-
-
board[row][col]="";
-
-
canvas.repaint(board, row, col);
-
-
-
-
}
-
-
/**
-
* Attempt to move a playing piece on the board
-
*@param move the proposed move
-
*@return TRUE if the move is valid, FALSE otherwise
-
*/
-
-
public boolean makeMove(String move){
-
-
-
for(int row=0; row<3; row++)
-
-
-
for(int col=0; col<3; col++)
-
-
-
if(board[row][col]==""){
-
-
board[row][col]=move;
-
-
return true;
-
-
}
-
-
else{
-
-
String msg = "Invalid move " + move + " entered.";
-
JOptionPane.showMessageDialog( null, msg, "Invalid Move", JOptionPane.ERROR_MESSAGE );
-
-
return false;
-
-
}
-
-
return false;
-
-
}
-
-
/**
-
*Prompt a player to move a playing piece on the board
-
*/
-
-
public String promptMove(){
-
-
currentPlayer=playerOne;
-
-
String inp = JOptionPane.showInputDialog( null, "Enter a valid move:" );
-
-
if(currentPlayer==playerOne){
-
-
return "X";
-
}
-
-
String inp1 = JOptionPane.showInputDialog( null, "Enter a valid move:" );
-
-
-
-
if(currentPlayer==playerTwo)
-
-
board[row][col]="O";
-
-
return "O";
-
-
-
}
-
-
-
public void quit(){
-
-
canvas.dispose();
-
-
System.exit(1);
-
}
-
-
public static void main(String args[]){
-
-
GameBoard gBoard=new TicTacToe();
-
-
gBoard.initBoard();
-
-
gBoard.promptMove();
-
-
gBoard.makeMove("X");
-
-
gBoard.displayBoard();
-
-
-
-
-
}
-
-
}
-
-
You get the user input as inpl, and then do absolutely nothing with it. You assume it magically gets transformed into a row and a col value. You need to decode inpl into row and col. Otherwise, you keep changing game[3][3], and the last entry should be game[2][2].
You get the user input as inpl, and then do absolutely nothing with it. You assume it magically gets transformed into a row and a col value. You need to decode inpl into row and col. Otherwise, you keep changing game[3][3], and the last entry should be game[2][2].
I am unclear as to what you have just said.
could someone plz help me with coding for interaction between client and server to run tictactoe.i need that as soon as possible..
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Az Tech |
last post by:
Hi people,
(Sorry for the somewhat long post).
I request some of the people on this group who have good experience
using object-orientation in the field, to please give some good ideas
for...
|
by: Nicky |
last post by:
hi,all
We are going to develop a program and when it is running, we need it full
screen and also, user can not switch to other place before exit our program.
I am thinking, we can make a window...
|
by: greg.landrum |
last post by:
After using numeric for almost ten years, I decided to attempt to
switch a large codebase (python and C++) to using numpy. Here's are
some comments about how that went.
- The code to...
|
by: chandniashar |
last post by:
This is my perl program for tictactoe. Can anyone help me run the program as a cgi script?
#!/usr/bin/perl
# Description: This program implements the game of Tic Tac Toe
# The grid...
|
by: wizardRahl |
last post by:
Hello,
I'm attempting to write a TicTacToe program for class and need some help with arrays. We have to write a program that will allow two users to play tic-tac-toe. The program needs to have...
|
by: Warren Hoskins |
last post by:
Old title: Homework Due 2-20-07
can"t understand why this will not compile. I've been working on tis all week end.
Need Help desperately
|
by: Luc The Perverse |
last post by:
Short version:
Trying to learn C# - 5 years C++, 2 years Java experience :) Help!
Long version:
I find "Learning C# 2005" by Jesse Liberty to be 'slow', to say the least.
I am skimming...
|
by: racshah |
last post by:
I have a tictactoe script with 2 users playing.
I need a perl script in which one user plays with the computer.
Can anyone help me with it???
|
by: Sean McIlroy |
last post by:
"""
AUTHOR: Sean McIlroy
LANGUAGE: Python 2.5
OVERVIEW: instances of "tictactoeplayer" play optimal tictactoe
SPECIALIZED TYPES: player={ex,oh}; empty={blank}; cell=player+empty;
board=
TYPE...
|
by: edgy |
last post by:
Hello,
I am a beginner with PHP, and I have made a language switcher on a site
that I am looking for your feedback on.
First of all, the page is http://www.mankar.ca
My question regarding...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |