473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

switching between 2 people in TicTacToe.java

14
How do you alternate between 2 human players?
Sep 29 '06 #1
6 12016
D_C
293 100+
Expand|Select|Wrap|Line Numbers
  1. int i;
  2. while((i <= 9) && (!win)) // 9 squares, so 9 inputs.
  3. {
  4.   System.out.println("Player " + (i%2) + "'s turn");
  5.   win = GetInputAndUpdate(((i%2)==1));
  6.   i++;
  7. }
  8. if(win)
  9. {
  10.   System.out.println("Player " + ((i-1)%2) + " wins!");
  11. }
  12.  
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).
Sep 30 '06 #2
yolkman
14
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.

Expand|Select|Wrap|Line Numbers
  1.     public boolean makeMove( String move ){
  2.  
  3.  
  4.         for(int row=0; row<3; row++){
  5.  
  6.  
  7.             for(int col=0; col<3; col++){
  8.  
  9.  
  10.             if(board[row][col]=="")
  11.             move.equals(board[row][col]);
  12.  
  13.             canvas.repaint(board, row, col);
  14.  
  15.             return true;
  16.  
  17.             }
  18.         }    
  19.  
  20.  
  21.             return false;
  22. }
  23.  
  24.    public String promptMove(){
  25.  
  26.         while(!gameOver()){
  27.  
  28.             currentPlayer=playerOne;
  29.  
  30.             if(currentPlayer.makeMove(String move))
  31.  
  32.             currentPlayer=playerTwo;
  33.  
  34.             if(currentPlayer.makeMove(String move))
  35.             currentPlayer=playerOne;
  36.  
  37.             if(currentPlayer.makeMove(String move))
  38.             currentPlayer=playerTwo;
  39.  
  40.             if(currentPlayer.makeMove(String move))
  41.             currentPlayer=playerOne;
  42.  
  43.             if(currentPlayer.makeMove(String move))
  44.             currentPlayer=playerTwo;
  45.  
  46.             if(currentPlayer.makeMove(String move))
  47.             currentPlayer=playerOne;
  48.  
  49.             if(currentPlayer.makeMove(String move))
  50.             currentPlayer=playerTwo;
  51.  
  52.             if(currentPlayer.makeMove(String move))
  53.             currentPlayer=playerOne;            
  54.  
  55.  
  56.  
  57.             }
  58.  
  59.  
  60.     }
  61.  
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"?
Oct 4 '06 #3
yolkman
14
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.

Expand|Select|Wrap|Line Numbers
  1. package csc216.project2;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5.  
  6.  
  7.  
  8. public class TicTacToe implements GameBoard{
  9.  
  10.     private GameCanvas canvas;
  11.  
  12.     private String [] [] board;
  13.  
  14.     private String currentPlayer;
  15.  
  16.     private String playerOne="X";
  17.  
  18.     private String playerTwo="O";
  19.  
  20.     private int row;
  21.  
  22.     private int col;
  23.  
  24.  
  25.  
  26.     public TicTacToe(){
  27.  
  28.         canvas=new GameCanvas();
  29.  
  30.         currentPlayer=null;
  31.  
  32.         board=new String[3][3];
  33.  
  34.         row=3;
  35.  
  36.         col=3;
  37.  
  38.  
  39.         }
  40.  
  41.     /**
  42.      *Draws current state of the board
  43.      */
  44.  
  45.     public void displayBoard(){
  46.  
  47.         canvas.repaint(board, row, col);
  48.  
  49.  
  50.  
  51.  
  52.         }
  53.  
  54.     /**
  55.      *Query whether the game is completed
  56.      *@return TRUE if game is over, FALSE otherwise
  57.      */            
  58.  
  59.     public boolean gameOver(){
  60.  
  61.         for(int row=0; row<3; row++ )
  62.             for(int col=0; col<3; col++)
  63.  
  64.  
  65.             if(board[0][col]=="X" || board[0][col]=="O")
  66.             return true;
  67.  
  68.             else if(board[1][col]=="X" || board[1][col]=="O")
  69.             return true;
  70.  
  71.             else if(board[2][col]=="X" || board[2][col]=="O")
  72.             return true;
  73.  
  74.             else if(board[row][0]=="X" || board[row][0]=="O")
  75.             return true;
  76.  
  77.             else if(board[row][1]=="X" || board[row][1]=="O")
  78.             return true;
  79.  
  80.             else if(board[row][2]=="X" || board[row][2]=="O")
  81.             return true;
  82.  
  83.             else if(board[0][0]=="X" && board[1][1]=="X" && board[2][2]=="X")
  84.             return true;
  85.  
  86.             else if(board[0][0]=="O" && board[1][1]=="O" && board[2][2]=="O")
  87.             return true;
  88.  
  89.             else if(board[2][0]=="X" && board[1][1]=="X" && board[0][2]=="X")
  90.             return true;
  91.  
  92.             else if(board[2][0]=="O" && board[1][1]=="O" && board[0][2]=="O")
  93.             return true;
  94.  
  95.             else if(board[row][col]!="")
  96.             return true;
  97.  
  98.  
  99.  
  100.             return false;
  101.  
  102.  
  103.  
  104.     }
  105.  
  106.     /**
  107.      *Initialize the board to a starting state
  108.      */
  109.  
  110.     public void initBoard(){
  111.  
  112.             for(int row=0; row<3; row++)
  113.  
  114.                 for(int col=0; col<3; col++)
  115.  
  116.                 board[row][col]="";
  117.  
  118.                 canvas.repaint(board, row, col);
  119.  
  120.  
  121.  
  122.     }
  123.  
  124.     /**
  125.      * Attempt to move a playing piece on the board
  126.      *@param move the proposed move
  127.      *@return TRUE if the move is valid, FALSE otherwise
  128.      */
  129.  
  130.     public boolean makeMove(String move){
  131.  
  132.  
  133.             for(int row=0; row<3; row++)
  134.  
  135.  
  136.                 for(int col=0; col<3; col++)
  137.  
  138.  
  139.                 if(board[row][col]==""){
  140.  
  141.                 board[row][col]=move;
  142.  
  143.                 return true;
  144.  
  145.                 }
  146.  
  147.                 else{
  148.  
  149.                 String msg = "Invalid move " + move + " entered.";
  150.                 JOptionPane.showMessageDialog( null, msg, "Invalid Move", JOptionPane.ERROR_MESSAGE );
  151.  
  152.                 return false;
  153.  
  154.                 }
  155.  
  156.                 return false;
  157.  
  158.     }
  159.  
  160.     /**
  161.      *Prompt a player to move a playing piece on the board
  162.      */
  163.  
  164.     public String promptMove(){
  165.  
  166.         currentPlayer=playerOne;
  167.  
  168.         String inp = JOptionPane.showInputDialog( null, "Enter a valid move:" );
  169.  
  170.         if(currentPlayer==playerOne){
  171.  
  172.         return "X";
  173.         }
  174.  
  175.         String inp1 = JOptionPane.showInputDialog( null, "Enter a valid move:" );
  176.  
  177.  
  178.  
  179.         if(currentPlayer==playerTwo)
  180.  
  181.         board[row][col]="O";
  182.  
  183.         return "O";
  184.  
  185.  
  186.     }
  187.  
  188.  
  189.     public void quit(){
  190.  
  191.         canvas.dispose();
  192.  
  193.         System.exit(1);
  194.     }
  195.  
  196.     public static void main(String args[]){
  197.  
  198.         GameBoard gBoard=new TicTacToe();
  199.  
  200.         gBoard.initBoard();
  201.  
  202.         gBoard.promptMove();
  203.  
  204.         gBoard.makeMove("X");
  205.  
  206.         gBoard.displayBoard();
  207.  
  208.  
  209.  
  210.  
  211. }
  212.  
  213. }
  214.  
  215.  
Oct 9 '06 #4
D_C
293 100+
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].
Oct 9 '06 #5
yolkman
14
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.
Oct 9 '06 #6
could someone plz help me with coding for interaction between client and server to run tictactoe.i need that as soon as possible..
Oct 12 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
6
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...
15
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...
0
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...
9
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...
7
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
13
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...
1
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???
0
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.