|Elflocko| Posted April 3, 2007 Report Share Posted April 3, 2007 [quote name='bengalrick' post='465595' date='Apr 1 2007, 06:19 PM']i have a database class this semester too, and i love it... i get oracle real well for whatever reason, but then again we are doing very basic stuff... but this java is supposidely basic too, and its driving me crazy[/quote] That means you were meant to be a DBA. Regular coding makes me want to drive a bus full of nuns off a cliff... So, do you find yourself building ERD's in your head everytime you see an application? Link to comment Share on other sites More sharing options...
Guest bengalrick Posted April 3, 2007 Report Share Posted April 3, 2007 [quote name='Elflocko' post='466217' date='Apr 3 2007, 12:19 AM']That means you were meant to be a DBA. Regular coding makes me want to drive a bus full of nuns off a cliff... So, do you find yourself building ERD's in your head everytime you see an application? [/quote] how'd you know that? Link to comment Share on other sites More sharing options...
|Elflocko| Posted April 3, 2007 Report Share Posted April 3, 2007 [quote name='bengalrick' post='466409' date='Apr 3 2007, 11:20 AM']how'd you know that? [/quote] If you're not, you're in the wrong field... Link to comment Share on other sites More sharing options...
Jrod Posted May 4, 2007 Author Report Share Posted May 4, 2007 This Java thread is now officially revived.... because I need help again. The object to this program is to create a tic tac toe game. I have quite a bit of code already thanks to help from others, but now I'm stuck. The parts I'm having trouble with are: - my method that controls the user's choice. Somehow I need to replace the array coordinate with the current user ("x" or "o") inside a switch statement and then re-display the board with that user's choice shown. We've been working with string manipulations such as .length(), .indexOf(), and .replace(). I can't seem to code that right. - I'm not such what I have to use to check for a win vertically and horizontally... I just don't understand the for loop within a for loop that I have to do to accomplish that. We did the diagonal check in class, but still....eh. - removing the user's choice (1-9) from the string cellsRemaining. I'll post the code below. [codebox]public class TicTacToe { public static void main(String[] args) { // create the board char[][] board = { {'1','2','3'} , {'4','5','6'} , {'7','8','9'} }; String cellsRemaining = "123456789"; String cellChoice = ""; int intCellChoice = 0; boolean gameOver = false; boolean didWin = false; boolean validChoice = false; Scanner sc = new Scanner(System.in); System.out.println("Welcome to the Tic Tac Toe Game"); drawBoard(board); // Could Code to ask which player is to start char currentPlayer = 'O'; // The oppiste player will have the first move // use a loop to check if a player won the game // or/and if any moves are left while(!gameOver && !didWin) { // switch which player has the next move if (currentPlayer == 'X') currentPlayer = 'O'; else currentPlayer = 'X'; // keep asking player for their move until they pick a good move // use a boolean flag to keep them in the loop while(!validChoice) { System.out.println("Player "+ currentPlayer + " please select your cell."); cellChoice = sc.next(); if(cellsRemaining.indexOf(cellChoice)==-1) { //not a valid selection System.out.println("Sorry, You made an invalid selection. Please try again"); } else { // need to remove the user choice from the string //********************************************************************* //********************************************************************** } } // reset your flag validChoice = false; // you know which cell the want so call a method that will place there mark (X or O) in the right cell // you will need to send the player the location and the table makeMove(intCellChoice, board, currentPlayer); //redraw thye board for the user drawBoard(board); // check to see if they won the game didWin = checkForWin(board,currentPlayer); // if they won tell them if(didWin) { System.out.println("Congradulations player "+ currentPlayer +" You have conquered you opponent."); } // check to see if game is over // by either checking for an X or O in every cell // or by checking your string for a zero length //********************************************************************* // you need to complete the if statement if () { //********************************************************************* gameOver=true; System.out.println("Looks like nobody won this time..."); } //gameOver = isGameOver(board); }// end while } // end main public static void drawBoard(char[][] board1) { //********************************************************************* //********************************************************************* } // Fill the array location with the letter of the player public static void makeMove(int cellNumber, char board1[][], char player) { switch (cellNumber) { //********************************************************************* //********************************************************************* } }// end makemove public static boolean checkForWin( char[][]board1, char player) { boolean isGameOver = false; // loop through vertically checking all wins in the array // If you find a win, set the isGameOver variable to true //********************************************************************* //********************************************************************* // loop through horizontally to check all parts of the array - same as // above //********************************************************************* //********************************************************************* // Special Case for diagonals // check and see - if middle is not yours no diagonals can exist if (board1[1][1]== player) { // left to right diagonal - easier case - (0,0)(1,1)(2,2) int count = 0; for (int x=0; x<3; x++) { if (board1[x][x] == player) { count++; } } if (count == 3) { isGameOver = true; } // THE UGLY STEP - RIGHT TO LEFT DIAGONAL!!! (2,0)(1,1)(0,2) if (board1[2][0]==player && board1[1][1]==player && board1[0][2]==player) { isGameOver = true; } } return isGameOver; }// end checkForWin() } [/codebox] Link to comment Share on other sites More sharing options...
Jamie_B Posted May 4, 2007 Report Share Posted May 4, 2007 Ill see if I can take a look at this this weekend if I have time, finals are next week so... Link to comment Share on other sites More sharing options...
Jamie_B Posted May 7, 2007 Report Share Posted May 7, 2007 Is this the only code you have? because I threw it into netbeans and Im getting cant find class on this line... [b]Scanner sc = new Scanner(System.in)[/b] and cant find method on this one... [b]drawBoard(board); [/b] If you have a scanner class i need the code for that as well. Link to comment Share on other sites More sharing options...
Jrod Posted May 7, 2007 Author Report Share Posted May 7, 2007 [quote name='Jamie_B' post='483489' date='May 7 2007, 12:17 PM']Is this the only code you have? because I threw it into netbeans and Im getting cant find class on this line... [b]Scanner sc = new Scanner(System.in)[/b] and cant find method on this one... [b]drawBoard(board); [/b] If you have a scanner class i need the code for that as well.[/quote] Well, that Scanner object error there might be just because the fact I did't have the java.util.Scanner imported in the code I pasted. I'll post my updated one..... the areas needed would be placed between the /***************************************** /***************************************** Those areas are where I'm kinda stuck. I have the ideas on what to do as I mentioned before, just don't know how to code it. [codebox]/* Jared Collum * TicTacToe * Created on May 3, 2007, 9:20 AM */ package collum; import java.util.Scanner; public class TicTacToe { public static void main(String[] args) { // create the board char[][] board = { {'1','2','3'} , {'4','5','6'} , {'7','8','9'} }; String cellsRemaining = "123456789"; String cellChoice = ""; int intCellChoice = 0; boolean gameOver = false; boolean didWin = false; boolean validChoice = false; Scanner sc = new Scanner(System.in); System.out.println("Welcome to the Tic Tac Toe Game"); drawBoard(board); // Could Code to ask which player is to start char currentPlayer = 'O'; // The oppiste player will have the first move // use a loop to check if a player won the game // or/and if any moves are left while(!gameOver && !didWin) { // switch which player has the next move if (currentPlayer == 'X') { currentPlayer = 'O'; } else { currentPlayer = 'X'; } // keep asking player for their move until they pick a good move // use a boolean flag to keep them in the loop while(!validChoice) { System.out.println("Player "+ currentPlayer + " please select your cell."); cellChoice = sc.next(); if(cellsRemaining.indexOf(cellChoice)==-1) { //not a valid selection System.out.println("Sorry, You made an invalid selection. Please try again"); } else { // need to remove the user choice from the string //********************************************************************* //********************************************************************** } } // End while // reset your flag validChoice = false; // you know which cell you want so call a method that will place thier mark (X or O) in the right cell // you will need to send the player the location and the table makeMove(intCellChoice, board, currentPlayer); //redraw the board for the user drawBoard(board); // check to see if they won the game didWin = checkForWin(board,currentPlayer); // if they won tell them if(didWin) { System.out.println("Congradulations player "+ currentPlayer +" You have conquered your opponent"); } // check to see if game is over // by either checking for an X or O in every cell // or by checking your string for a zero length // you need to complete the if statement if(cellsRemaining.equals("")) { gameOver=true; System.out.println("Looks like nobody won this time..."); } //gameOver = isGameOver(board); }// end while } // end main public static void drawBoard(char[][] board1) { System.out.println(" | |"); System.out.println(" "+ board1[0][0]+" | "+ board1[0][1]+" | "+board1[0][2]+" "); System.out.println(" | |"); System.out.println("----------------+---------------+---------------"); System.out.println(" | |"); System.out.println(" "+ board1[1][0]+" | "+ board1[1][1]+" | "+board1[1][2]+" "); System.out.println(" | |"); System.out.println("----------------+---------------+---------------"); System.out.println(" | |"); System.out.println(" "+ board1[2][0]+" | "+ board1[2][1]+" | "+board1[2][2]+" "); System.out.println(" | |"); } // Fill the array location with the letter of the player public static void makeMove(int cellNumber, char board1[][], char player) { switch(cellNumber) { //********************************************************************* case 1: //********************************************************************* } }// end makemove public static boolean checkForWin(char[][]board1, char player) { boolean isGameOver = false; // loop through vertically checking all wins in the array // If you find a win, set the isGameOver variable to true //********************************************************************* //********************************************************************* // loop through horizontally to check all parts of the array - same as // above //********************************************************************* //********************************************************************* // Special Case for diagonals // check and see - if middle is not yours no diagonals can exist if (board1[1][1]== player) { // left to right diagonal - easier case - (0,0)(1,1)(2,2) int count = 0; for (int x=0; x<3; x++) { if (board1[x][x] == player) { count++; } } if (count == 3) { isGameOver = true; } // THE UGLY STEP - RIGHT TO LEFT DIAGONAL!!! (2,0)(1,1)(0,2) if (board1[2][0]==player && board1[1][1]==player && board1[0][2]==player) { isGameOver = true; } } return isGameOver; }// end checkForWin() } // end Class [/codebox] Link to comment Share on other sites More sharing options...
Jamie_B Posted May 7, 2007 Report Share Posted May 7, 2007 Ok the import got rid of the 1st issue, but the drawBoard(board) one is still there Link to comment Share on other sites More sharing options...
Jrod Posted May 7, 2007 Author Report Share Posted May 7, 2007 [quote name='Jamie_B' post='483502' date='May 7 2007, 12:36 PM']Ok the import got rid of the 1st issue, but the drawBoard(board) one is still there[/quote] Did you try the code I just put up? I'm pretty sure that runs, it's just not finished of course. ..Yeah, the second code I put up runs. I get the game board up but you can't play yet, thats where the string and array manipulation comes in. Link to comment Share on other sites More sharing options...
Jamie_B Posted May 7, 2007 Report Share Posted May 7, 2007 check that it was an issue of copying and pasting it copied it into one line for all of the code and once i cleaned that up it fixed it. Ill see what I can figure out. But like I said I have finals this week so Im pressed for time. Link to comment Share on other sites More sharing options...
Jrod Posted May 7, 2007 Author Report Share Posted May 7, 2007 [quote name='Jamie_B' post='483506' date='May 7 2007, 12:45 PM']check that it was an issue of copying and pasting it copied it into one line for all of the code and once i cleaned that up it fixed it. Ill see what I can figure out. But like I said I have finals this week so Im pressed for time.[/quote] No, I understand. I got finals all this week too, including getting this final program project done. Calculus, Java, COBOL, Bio... I'm swamped, but I appreciate you at least lending a hand. Link to comment Share on other sites More sharing options...
Jrod Posted May 9, 2007 Author Report Share Posted May 9, 2007 Well, I figured it all out, completed the project, and submitted it in. Everything ran ok; it checked vertical, horizontal, and diagonal wins and recognized a player win. ...done with Java... for now I guess. Link to comment Share on other sites More sharing options...
Jamie_B Posted May 9, 2007 Report Share Posted May 9, 2007 Sorry I didnt get to it, its been a crazy week. Link to comment Share on other sites More sharing options...
Guest bengalrick Posted May 9, 2007 Report Share Posted May 9, 2007 [quote name='Jrod' post='484483' date='May 9 2007, 02:31 PM']Well, I figured it all out, completed the project, and submitted it in. Everything ran ok; it checked vertical, horizontal, and diagonal wins and recognized a player win. ...[b]done with Java... for now I guess.[/b][/quote] i know the feeling... <insert smiley banging head against wall> Link to comment Share on other sites More sharing options...
Jamie_B Posted May 11, 2007 Report Share Posted May 11, 2007 Good thing the semester is over and I was wrong about my summer class being a java class (it's PHP and MySql). As I got busted for having Netbeans on my PC here at work. Damn goverment and their "approved" software list. Link to comment Share on other sites More sharing options...
|Elflocko| Posted May 11, 2007 Report Share Posted May 11, 2007 [quote name='Jamie_B' post='485537' date='May 11 2007, 03:29 PM']Good thing the semester is over and I was wrong about my summer class being a java class (it's [b]PHP and MySql)[/b]. As I got busted for having Netbeans on my PC here at work. Damn goverment and their "approved" software list. [/quote] I'm predicting that you will find it the easiest programming language you've tried so far.... Link to comment Share on other sites More sharing options...
Jamie_B Posted May 11, 2007 Report Share Posted May 11, 2007 [quote name='Elflocko' post='485558' date='May 11 2007, 05:16 PM'] I'm predicting that you will find it the easiest programming language you've tried so far....[/quote] Hope so, its a 16 week course shoved into a 2 month time frame. Got the book yesterday, Ill probabally look over it next week before class starts at the begining of June. Link to comment Share on other sites More sharing options...
|Elflocko| Posted May 11, 2007 Report Share Posted May 11, 2007 [quote name='Jamie_B' post='485561' date='May 11 2007, 04:20 PM']Hope so, its a 16 week course shoved into a 2 month time frame. Got the book yesterday, Ill probabally look over it next week before class starts at the begining of June.[/quote] Larry Ullman's [i]PHP and MySQL for Dynamic Web Sites[/i] is a kick-ass reference\tutorial. And as I've stated previously, I'm a real shitty programmer, and [b]I[/b] get it, so you should have no problems. Link to comment Share on other sites More sharing options...
Jamie_B Posted May 11, 2007 Report Share Posted May 11, 2007 [quote name='Elflocko' post='485586' date='May 11 2007, 06:37 PM']Larry Ullman's [i]PHP and MySQL for Dynamic Web Sites[/i] is a kick-ass reference\tutorial. And as I've stated previously, I'm a real shitty programmer, and [b]I[/b] get it, so you should have no problems.[/quote] [img]http://g-ec2.images-amazon.com/images/I/51LbNyaV08L._AA240_.jpg[/img] This is the book were using. [url="http://www.amazon.com/o/ASIN/0619216875/ref=s9_asin_image_1-2288_p/104-1084050-5411129?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-1&pf_rd_r=0NMQJ1RZ3E2ZDHKB3B6B&pf_rd_t=101&pf_rd_p=288448401&pf_rd_i=507846"]http://www.amazon.com/o/ASIN/0619216875/re...;pf_rd_i=507846[/url] Link to comment Share on other sites More sharing options...
|Elflocko| Posted May 12, 2007 Report Share Posted May 12, 2007 [quote name='Jamie_B' post='485590' date='May 11 2007, 05:48 PM'][img]http://g-ec2.images-amazon.com/images/I/51LbNyaV08L._AA240_.jpg[/img] This is the book were using. [url="http://www.amazon.com/o/ASIN/0619216875/ref=s9_asin_image_1-2288_p/104-1084050-5411129?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-1&pf_rd_r=0NMQJ1RZ3E2ZDHKB3B6B&pf_rd_t=101&pf_rd_p=288448401&pf_rd_i=507846"]http://www.amazon.com/o/ASIN/0619216875/re...;pf_rd_i=507846[/url][/quote] I've found that actual textbooks are rarely conducive to learning... Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.