473,378 Members | 1,401 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,378 software developers and data experts.

Tic Tac Toe issue

Steel546
So I'm working on my final lab, and I'm fixing a few errors. But the one I can't get right now is how to return who is the winner. Everytime I try to write the code for findWinner, it says missing return value, despite it being (what I think is) correct. There's two classes. And yes, there's an Infinite loop I'm trying to find, but I'll get to that next.

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class TicTacToeGameFirstLast {
  3.  
  4.     public static final int board_size = 3;       // number of rows or columns in the board
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         char board[][] = new char[board_size][board_size];  // the game board
  9.  
  10.         TicTacToePlayerFirstLast p1 = new TicTacToePlayerFirstLast();  // create the players
  11.         TicTacToePlayerFirstLast p2 = new TicTacToePlayerFirstLast();
  12.  
  13.         initBoard (board);                 // initialize the board to spaces and print it out
  14.         displayBoard (board);
  15.  
  16.         char winner = findWinner(board);
  17.  
  18.         while (winner == ' ') {
  19.  
  20.             p1.getMove(board, 'X');   // player one will be 'X', and will always go first
  21.  
  22.             displayBoard (board);
  23.  
  24.             winner = findWinner(board);
  25.  
  26.             if (winner != ' ') {
  27.                 if (winner == 'T') {     System.out.println("Tie Game!");      }
  28.                 else {        System.out.println("The Winner is: " + winner);  }
  29.                 break;
  30.             }
  31.  
  32.             p2.getMove(board, 'O');   // player two will be 'O', and always go second
  33.  
  34.             displayBoard (board);
  35.  
  36.             winner = findWinner(board);
  37.  
  38.             if (winner != ' ') {
  39.                 if (winner == 'T') {     System.out.println("Tie Game!");      }
  40.                 else {        System.out.println("The Winner is: " + winner);  }
  41.             }
  42.         }
  43.  
  44.     }
  45.  
  46.     public static void initBoard(char[][] theBoard) {
  47.         // since this is pass by reference, initializing the Board here resets it in main
  48.  
  49.         for (int row = 0; row < board_size; row++) {
  50.             for (int col = 0; col < board_size; col++) {
  51.                 theBoard[row][col] = ' ';                   // row always comes first in a 2d array
  52.             }
  53.         }
  54.  
  55.     }
  56.  
  57.  
  58.     public static void displayBoard(final char[][] theBoard) {
  59.         // note the final parameter - we won't change the board, just print it.
  60.  
  61.         // Need to format the board display to look like this:
  62.         //  | |
  63.         // -----
  64.         //  | |
  65.         // -----
  66.         //  | |
  67.  
  68.         for (int row = 0; row < board_size; row++) {
  69.             for (int col = 0; col < board_size; col++) {
  70.                 System.out.print("|" + theBoard[row][col] + "|");                   // row always comes first in a 2d array
  71.             }
  72.             System.out.println("\n---------");
  73.         } 
  74.     }
  75.  
  76.     public static char findWinner(final char[][] theBoard){
  77.  
  78.  
  79.         // this method should return 'X' if X is the winner
  80.         // 'O' if O is the winner, a space (' ') if there is no winner,
  81.         // or 'T' if there is a tie. For now it just returns a space no matter what.
  82.  
  83.         if (theBoard[0][0] == 'X' && theBoard[0][1] =='X' && theBoard[0][2] == 'X'){
  84.  
  85.             return theBoard[0][0];
  86.  
  87.         }
  88.  
  89.     }
  90.  
  91.  }
  92.  
Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /**
  7.  *
  8.  * @author Ryan Garlick
  9.  */
  10. import java.util.Random;
  11.  
  12. public class TicTacToePlayerFirstLast {
  13.  
  14.     public void getMove(char[][] theBoard, char myPiece) {
  15.  
  16.         // this method should update the game board to place 'myPiece' (X or O)
  17.         // in a space on the board.
  18.  
  19.         // As-is, this method just places an X or O randomly, without even considering
  20.         // if the square is already taken.
  21.  
  22.         Random myRand = new Random();
  23.  
  24.         int myRow = myRand.nextInt(3);
  25.         int myCol = myRand.nextInt(3);
  26.  
  27.         theBoard[myRow][myCol] = myPiece;      
  28.  
  29.     }
  30.  
  31. }
  32.  
May 1 '09 #1
1 2451
JosAH
11,448 Expert 8TB
@Steel546
What does your method return if the test in that if-statement is false? Indeed, you don't return anything and your compiler was clever enough to figure that out.

kind regards,

Jos

ps. Also read an article on Tic Tac Toe in the 'advanced' math section; it contains a simple method that checks for a winner.
May 2 '09 #2

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

Similar topics

3
by: Paul Mateer | last post by:
Hi, I have been running some queries against a table in a my database and have noted an odd (at least it seems odd to me) performance issue. The table has approximately 5 million rows and...
7
by: George Hester | last post by:
Please take a look at this google artcle: http://groups.google.com/groups?hl=en&lr=&frame=right&th=55d6f4b50f5f9382&seekm=411f370d%241%40olaf.komtel.net#link9 The op was having trouble with...
2
by: Anthony Cuttitta Jr. | last post by:
We have an application that outputs several different graphs from data downloaded from our AS400. The application has worked without (this) issue for several months now, but just recently, the...
0
by: Kevin Spencer | last post by:
Hi all, I am working on a service that uploads METAR weather information to the National Weather Service FTP site. The service I'm authoring is hosted on a Windows 200 server, and the NWS FTP...
2
by: Ben Rush | last post by:
Hello World, Okay, I have spent the day browsing the newsgroups and reading up on article after article concerning ViewState corruption and so forth, and I have a couple questions. We...
5
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant...
0
by: Charles Leonard | last post by:
I am having yet another issue with Windows Server 2003. This time, the web service (a file import web service) appears to run except for one odd message: "ActiveX component can't create object". ...
4
by: Paul | last post by:
Hi, I've been struggling with this today, I'm developing a DotNet2.0 website in C# that needs to call a long running data query. Obviously this is a good candidate for an Asynchronous call, so...
1
by: AlekseyUS | last post by:
Hi, I'm a little stuck, I basically need to copy all the information within a specific file in Temp and append it to a file in another location. I'm not having any problems with smaller size...
13
by: SAL | last post by:
Hello, I'm trying to include a popup in the ItemTemplate of a gridview row. The ItemTemplate for the field contains a textbox and when the user clicks in the textbox I want a popup panel to show...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.