Jump to content
  • Advertisement

jim8ean

Member
  • Content Count

    15
  • Joined

  • Last visited

Community Reputation

122 Neutral

About jim8ean

  • Rank
    Member
  1. Hey Howard, this is Dustin, I posted this question for some external help, sorry for the misconception!
  2. Very helpful! Ill give it a try later today and post back how I make out.
  3. Okay I am going be honest so this is for my final project and I have to have a running server and two connected clients. I could get this done if I wasn't under time restraints cause I'm a full time online student and I have other finals to work to finish up and physics work.    I really just need as much feedback and fix what I can fix before I have to submit this at the end of today and the flickering is my main concern then the collision detection (which shouldn't be too bad if I could spent enough time staring at it).    I will try again to convert back into a JFrame but I tried once and I had trouble getting my existing components to draw to my frame. * I dont know where to create the connected clients frame (at in my clients main?) * I dont know where to manage the frame and the drawed component   * I dont know how to start the running thread of the client and the client's input's source using the same instance of the frame and the clients game(it seemed more straight forward at first using the applet)     I wish I started this using a Jframe my stupid mistake. Anyways thanks hplus0603 your suggestions gave me better insurance I what actions I should try to take!
  4. I forgot to include the player images so here is the re-upload now you can see the players.
  5. Awesome thanks for those first tips, I might have posted this problem in the wrong forum but thought it seemed somewhat appropriate here.     I have built successful single players on a local machine with Java, C# and I made a couple using html and javascript but never working with sockets and multiple connecting clients.   I'm not actually trying to make a browser game either although it does look like I am using an applet. I'd love to transfer this into a JFrame if anyone has any input on how I can approach that because I can see that my uploaded game has 6 downloads already. 
  6. I need some pointers on how to stop this applet from flickering that's driving me mad! I had some trouble setting this up as a JFrame so I continued working on it as an applet (probably my main mistake). I'm relatively new with handling data on and off a server.    Also need some advice on how to approach this simple program, my code feels messy and don't know the proper way to set this up (my book is no help).    I neglected to include any commenting in my java files besides for what I written below. public class Client extends Applet implements Runnable, KeyListener { public void init() { //Start everything and establish a connection with the server // Start this thread // Start thread that gets the data back from the server } public void updateCoordinates(int pid, int y2, int bxin, int byin, int lives2, boolean b) { // Update new values retrieved from server // About to be re-painted } // Where the flickering may ocurr public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; for(int i = 0; i < playerLimit; i++) { if(i % 2 == 0) { g.drawImage(playerImgLeft, x[i], y[i], 80, 80, null); if(shoot[i]==true) bullet = new Ellipse2D.Double(bx[i]+35, by[i]+35, 10, 10); } if(i % 2 != 0) { g.drawImage(playerImgRight, x[i], y[i], 80, 80, null); if(shoot[i]==true) bullet = new Ellipse2D.Double(bx[i]+35, by[i]+35, 10, 10); } g2.fill(bullet); g2.draw(bullet); } // Draw stuff to the screen } private void hits() { // When the bullet falls within range of a player lives-- // NOT WORKING if( ((locX+80 <= locBx) && (locX <= locBx+10)) && ((locY+80 >= locBy+10) && (locY <= locBy)) ) lives--; } public void run() { while(true) { // Check if a player was hit by the bullet // Check if player moves up // Check if player moves down // Check if it fired // If fired let bullet finish run // Disable bullet once it leaves range // Update bullet if(up || down || b) { // Write the data out to the server } repaint(); // Sleep } } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } } class Input implements Runnable { public Input(DataInputStream in, Client c) { } public void run() { // Get Feedback back from the server // Update locations } } ////////////***********************************//////////////// // And there's a server .java file that loops through both or more users and does work on each user so they both see the same thing. Any input to one or both of my problems would be greatly appreciated!  
  7. Come on! I busted my balls to make this functional! Please just give me advice on how to create an algorithm for four in a row, I am soooo close. Besides this is my first year coding.
  8. Okay I'm pleased to note with some of your guy's help I got everything working except for checking for 4 in a row. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Connect4 { class Program { static int row0 = 6, row1 = 6, row2 = 6, row3 = 6, row4 = 6, row5 = 6, row6 = 6; static int checker; static void Main(string[] args) { DisplayInstructions(); Play(); } public static void Play() { char[,] board = new char[6, 7]; //Create board. int rows = 5; //I dont know if I need these int columns = 6; //and this checker = 0; int rowCheck = 0; Board(board, rows, columns); //Fills board with +'s. PrintBoard(board, rows, columns, rowCheck); //Display's board PutPiece(board, rows, columns, rowCheck); //Start to play. Console.ReadKey(); } //Checks input for validation might need better way to check the input. public static int ColumnInput() { string inColumnNum; int columnPicked; inColumnNum = Console.ReadLine(); while (int.TryParse(inColumnNum, out columnPicked) == false) { Console.WriteLine("Invalid input"); Console.Write("Please re-enter an column between 0 and 6. \n"); inColumnNum = Console.ReadLine(); } if ((columnPicked < 0) || (columnPicked > 6)) { Console.WriteLine("Please pick the appropriate column"); } return columnPicked; } public static int PlayerTurn(int Player) { if (Player == 0) { return Player; } else if (Player == 1) { return Player; } return Player; } //Checks for full board if so then declares as true. public static Boolean FullBoard() { bool fullCheck = false; Console.WriteLine(checker); if (checker == 42) { fullCheck = true; } return fullCheck; } public static int Win(char[,] board, int x, int y) { //Algorythm? } public static void PutPiece(char[,] board, int rows, int columns, int rowCheck) { while (FullBoard() != true) { if (PlayerTurn(0) == 0) { Console.WriteLine("Player 1 turn."); int column = ColumnInput(); if (column == 0) { int num = UpdateCol1(row0); board[num, column] = 'X'; } if (column == 1) { int num = UpdateCol2(row1); board[num, column] = 'X'; } if (column == 2) { int num = UpdateCol3(row2); board[num, column] = 'X'; } if (column == 3) { int num = UpdateCol4(row3); board[num, column] = 'X'; } if (column == 4) { int num = UpdateCol5(row4); board[num, column] = 'X'; } if (column == 5) { int num = UpdateCol6(row5); board[num, column] = 'X'; } if (column == 6) { int num = UpdateCol7(row6); board[num, column] = 'X'; } Win(board, rows, columns); PrintBoard(board, rows, columns, rowCheck); PlayerTurn(1); } if (PlayerTurn(1) == 1) { Console.WriteLine("Player 2 turn."); int column = ColumnInput(); if (column == 0) { int num = UpdateCol1(row0); board[num, column] = 'O'; } if (column == 1) { int num = UpdateCol2(row1); board[num, column] = 'O'; } if (column == 2) { int num = UpdateCol3(row2); board[num, column] = 'O'; } if (column == 3) { int num = UpdateCol4(row3); board[num, column] = 'O'; } if (column == 4) { int num = UpdateCol5(row4); board[num, column] = 'O'; } if (column == 5) { int num = UpdateCol6(row5); board[num, column] = 'O'; } if (column == 6) { int num = UpdateCol7(row6); board[num, column] = 'O'; } PrintBoard(board, rows, columns, rowCheck); Win(board, rows, columns); PlayerTurn(0); } } PromptToPlayAgain(); } public static int UpdateCol1(int rowNum) { if (row0 != 0) { checker++; row0--; } else { Console.WriteLine("This column is full, choose another."); ColumnInput(); } return row0; } public static int UpdateCol2(int rowNum) { if (row1 != 0) { checker++; row1--; } else { Console.WriteLine("This column is full, choose another."); ColumnInput(); } return row1; } public static int UpdateCol3(int rowNum) { if (row2 != 0) { checker++; row2--; } else { Console.WriteLine("This column is full, choose another."); ColumnInput(); } return row2; } public static int UpdateCol4(int rowNum) { if (row3 != 0) { checker++; row3--; } else { Console.WriteLine("This column is full, choose another."); ColumnInput(); } return row3; } public static int UpdateCol5(int rowNum) { if (row4 != 0) { checker++; row4--; } else { Console.WriteLine("This column is full, choose another."); ColumnInput(); } return row4; } public static int UpdateCol6(int rowNum) { if (row5 != 0) { checker++; row5--; } else { Console.WriteLine("This column is full, choose another."); ColumnInput(); } return row5; } public static int UpdateCol7(int rowNum) { if (row6 != 0) { checker++; row6--; } else { Console.WriteLine("This column is full, choose another."); ColumnInput(); } return row6; } //Fills the empty array. public static void Board(char[,] board, int rows, int columns) { for (int i = 0; i < 6; i++) { for (int z = 0; z < 7; z++) { board[i, z] = '+'; } } } //Will print the board when called. public static void PrintBoard(char[,] board, int rows, int columns, int rowCheck) { Console.WriteLine("Current Board, Player 1 = X, Player 2 = O. \n"); for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { Console.Write(board[i, j]); } Console.WriteLine(); } } //Will ask if your done player when called. public static void PromptToPlayAgain() { DialogResult Result = MessageBox.Show("Lets play again!", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (Result == DialogResult.Yes) { Play(); } else { EndGame(); } } //Ends the game. public static void EndGame() { Console.WriteLine("You chose to end the game.\nThis game is going to close in 4 seconds..."); System.Threading.Thread.Sleep(4000); System.Environment.Exit(0); } //Displays the instructions once. public static void DisplayInstructions() { Console.WriteLine("I'm glad you've chosen to play Connect 4 with X's and O's!\nTo play enter the row player X or player O wants to put his/hers piece."); } } }
  9. My Board is going to need to know... -The size of the array -What index's are empty -When a column is full -When the board is full -When is there 4 in a row -Whose turn it is -Is there a tie -Who is the winner That's all I can think of that my board class should keep track of Alvero? And thanks StarMire for your input. I'm going to give your thoughts a go tomorrow, I need a fresh start working on this. I don't know why I've been using X's and O's hah.
  10. Okay thanks for your reply but what you said is what I already figured. I need help setting up my new class 'Board' methods if that's the case. So I create my array in a method in my 'Board' class and then what methods do I fill it with like I said, I haven't worked with more than one class very much in my classes. Here's what I'm starting with... class Board { public Board() { } public void BoardArray() { char[,] board = new char[6, 7]; } public void AvailableMoves() { } public void MakeMove() { } }
  11. I've been trying to build this game with a friend and so far he hasn't been much help so I am going to ask this community for suggestions and advice for building my Connect4 console game. So far I am filling the guts, I haven't put a method to check for 4 in a row yet and for both of the user's inputs. Also I don't know how to make my Move method start so you cant start dropping in the pieces.   I left a few notes in my comments on whether I should implement another class to store my row0,1,2,3,4,5,6 to see if my column is full or not.   I just want a more efficient ideas to create my code. I keep wanting to add more arguments in it!   If I should create another class could someone guide me on how I should do that with what I got, I'm not very familiar with gets, sets, values but it seems like I might need to use this?   I apologize if there's too much code here.   Much appreciated, -Dustin using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Connect4 { class Program { static void Main(string[] args) { DisplayInstructions(); Play(); } public static void Play() { char[,] board = new char[6, 7]; //Create board. int rows = 5; //I dont know if I need these int columns = 6; //and this bool checkForFull = false; //Initialize method objects. int rowCheck = 0; Board(board, rows, columns); //Fills board with +'s. PrintBoard(board, rows, columns, checkForFull, rowCheck); //Display's board //char playerX = 'X'; //char playerO = 'O'; Move(board, rows, columns, checkForFull, rowCheck); //Start to play. Console.ReadKey(); } //Checks input for validation might need better way to check the input. public static int PlayerTurn() { string inColumnNum; int columnPicked; inColumnNum = Console.ReadLine(); columnPicked = int.Parse(inColumnNum); if (columnPicked >= 6 || columnPicked <= 1) { Console.WriteLine("Please pick the appropriate column"); //Move(playerX, playerO); } return columnPicked; } //Checks for full board if so then declares as true. public static void FullBoard(char[,] board, int rows, int columns, bool checkForFull, int rowCheck) { int total = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { if (board[i, j] == 'X' || board[i, j] == 'O') { total += 1; if (total == 42) { checkForFull = true; PrintBoard(board, rows, columns, checkForFull, rowCheck); Console.WriteLine("\nIt's a tie!"); PromptToPlayAgain(); } } } } checkForFull = false; } public static void ColumnDepthKeeper(int rowCheck) { if (rowCheck == 0) { //Probably going to need another method to keep track of column depth } } public static char[,] Move(char[,] board, int rows, int columns, bool checkForFull, int rowCheck) { int row0 = 6; int row1 = 6; int row2 = 6;//Might have to go into another class to be stored becasue it will keep reseting if its placed here and cant be initialized outside the static class int row3 = 6;//Unless you want 7 methods or if you know a more effecient way of doing it? int row4 = 6; int row5 = 6; int row6 = 6; PlayerTurn();//I dont know how to start the game? while (checkForFull == false) { if (PlayerTurn() == 0) //Checks if player chooses column 1. { if (row0 == 0) //Checks if column 1 is full. { Console.WriteLine("This column is full please choose an empty slot."); return Move(board, rows, columns, checkForFull, rowCheck); } else //If not, adds new peice. { board[row0, PlayerTurn()] = 'X'; //Sets picked column and bottom of row with X or O. row0--; //Update the row stored... This needs to come from another classs somehow? row0 = rowCheck; //Sends to method to check if full ColumnDepthKeeper(rowCheck); PrintBoard(board, rows, columns, checkForFull, rowCheck); //Show the board again. return Move(board, rows, columns, checkForFull, rowCheck); //Run through new input. } } else if (PlayerTurn() == 1) { if (row1 == 0) { Console.WriteLine("This column is full please choose an empty slot."); return Move(board, rows, columns, checkForFull, rowCheck); } else { board[row1, PlayerTurn()] = 'X'; row1--; row1 = rowCheck; ColumnDepthKeeper(rowCheck); PrintBoard(board, rows, columns, checkForFull, rowCheck); return Move(board, rows, columns, checkForFull, rowCheck); } } else if (PlayerTurn() == 2) { if (row2 == 0) { Console.WriteLine("This column is full please choose an empty slot."); return Move(board, rows, columns, checkForFull, rowCheck); } else { board[row2, PlayerTurn()] = 'X'; row2--; row2 = rowCheck; ColumnDepthKeeper(rowCheck); PrintBoard(board, rows, columns, checkForFull, rowCheck); return Move(board, rows, columns, checkForFull, rowCheck); } } else if (PlayerTurn() == 3) { if (row3 == 0) { Console.WriteLine("This column is full please choose an empty slot."); return Move(board, rows, columns, checkForFull, rowCheck); } else { board[row3, PlayerTurn()] = 'X'; row3--; row3 = rowCheck; ColumnDepthKeeper(rowCheck); PrintBoard(board, rows, columns, checkForFull, rowCheck); return Move(board, rows, columns, checkForFull, rowCheck); } } else if (PlayerTurn() == 4) { if (row4 == 0) { Console.WriteLine("This column is full please choose an empty slot."); return Move(board, rows, columns, checkForFull, rowCheck); } else { board[row4, PlayerTurn()] = 'X'; row4--; row4 = rowCheck; ColumnDepthKeeper(rowCheck); PrintBoard(board, rows, columns, checkForFull, rowCheck); return Move(board, rows, columns, checkForFull, rowCheck); } } else if (PlayerTurn() == 5) { if (row5 == 0) { Console.WriteLine("This column is full please choose an empty slot."); return Move(board, rows, columns, checkForFull, rowCheck); } else { board[row4, PlayerTurn()] = 'X'; row5--; row5 = rowCheck; ColumnDepthKeeper(rowCheck); PrintBoard(board, rows, columns, checkForFull, rowCheck); return Move(board, rows, columns, checkForFull, rowCheck); } } else if (PlayerTurn() == 6) { if (row6 == 0) { Console.WriteLine("This column is full please choose an empty slot."); return Move(board, rows, columns, checkForFull, rowCheck); } else { board[row6, PlayerTurn()] = 'X'; row6--; row6 = rowCheck; ColumnDepthKeeper(rowCheck); PrintBoard(board, rows, columns, checkForFull, rowCheck); return Move(board, rows, columns, checkForFull, rowCheck); } } return Move(board, rows, columns, checkForFull, rowCheck); } Console.WriteLine("Game Over."); PromptToPlayAgain(); //When the board is full and check for full = true then ask to play again. return Move(board, rows, columns, checkForFull, rowCheck); //Need this just cause. } //Fills the empty array. public static void Board(char[,] board, int rows, int columns) { for (int i = 0; i < 6; i++) { for (int z = 0; z < 7; z++) { board[i, z] = '+'; } } } //Will print the board when called. public static void PrintBoard(char[,] board, int rows, int columns, bool checkForFull, int rowCheck) { { Console.WriteLine("Current Board (user1 = O, user2 = X): \n"); for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { Console.Write(board[i, j]); } Console.WriteLine(); } } } //Will ask if your done player when called. public static void PromptToPlayAgain() { DialogResult Result = MessageBox.Show("Lets play again!", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (Result == DialogResult.Yes) { Play(); } else { EndGame(); } } //Ends the game. public static void EndGame() { Console.WriteLine("You chose to end the game.\nThis game is going to close in 4 seconds..."); System.Threading.Thread.Sleep(4000); System.Environment.Exit(0); } //Displays the instructions once. public static void DisplayInstructions() { Console.WriteLine("I'm glad you've chosen to play Connect 4!\nTo play enter the row player X or player O wants to put his/hers piece."); } } }
  12. I am pleased to note that I got full functionality to my program!! Besides my exhaustion doesn't update for some reason and my starves doesn't increment. Thanks for the help BearNutts for pointing my obvious mistakes. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace IslandDrunk { class Program { static void Main(string[] args) { int lives = 0, drowns = 0, starves = 0; Console.WriteLine("All you need to know... \n1 moves drunk up, 2 moves drunk right, 3 moves drunk down, 4 moves drunk left"); Play(lives, drowns, starves); } public static int NumRows() { int inNumRows; String inStringValue; inStringValue = Console.ReadLine(); while (int.TryParse(inStringValue, out inNumRows) == false) { Console.WriteLine("Invalid input"); Console.Write("Please re-enter an integer value between 3 and 25. \n"); inStringValue = Console.ReadLine(); } if ((inNumRows <= 3) || (inNumRows >= 25)) { Console.WriteLine("Invalid input"); Console.Write("Please re-enter an integer value between 3 and 25. \n"); inStringValue = Console.ReadLine(); } return inNumRows; } public static int NumColumns() { int inNumColumns; String inStringValue; inStringValue = Console.ReadLine(); while (int.TryParse(inStringValue, out inNumColumns) == false) { Console.WriteLine("Invalid input"); Console.Write("Please re-enter an integer value between 3 and 25. \n"); inStringValue = Console.ReadLine(); } if((inNumColumns <= 3) || (inNumColumns >= 25)) { Console.WriteLine("Invalid input"); Console.Write("Please re-enter an integer value between 3 and 25. \n"); inStringValue = Console.ReadLine(); } return inNumColumns; } public static int NumBridges() { int inNumBridges; String inStringValue; inStringValue = Console.ReadLine(); while (int.TryParse(inStringValue, out inNumBridges) == false) { Console.WriteLine("Invalid input"); Console.Write("Please re-enter an integer value between 1 and 25. \n"); inStringValue = Console.ReadLine(); } if ((inNumBridges <= 0) || (inNumBridges >= 25)) { Console.WriteLine("Invalid input"); Console.Write("Please re-enter an integer value between 1 and 25. \n"); inStringValue = Console.ReadLine(); } return inNumBridges; } public static int NumExhaustion() { int inNumExhaustion; String inStringValue; inStringValue = Console.ReadLine(); while (int.TryParse(inStringValue, out inNumExhaustion) == false) { Console.WriteLine("Invalid input"); Console.Write("Please re-enter an integer value between 1 and 25. \n"); inStringValue = Console.ReadLine(); } if ((inNumExhaustion <= 0) || (inNumExhaustion >= 25)) { Console.WriteLine("Invalid input"); Console.Write("Please re-enter an integer value between 1 and 25. \n"); inStringValue = Console.ReadLine(); } return inNumExhaustion; } public static void Play(int lives, int drowns, int starves) { Console.WriteLine("\nPlease enter the amount of rows you want for your island, \nmust be greater than 3, less than 25."); int rows = NumRows(); Console.WriteLine("\nPlease enter the amount of columns you want for your island, \nmust be greater than 3, less than 25."); int columns = NumColumns(); Console.WriteLine("\nPlease enter the number of bridges you prefer."); int bridgeCount = NumBridges(); Console.WriteLine("\nPlease enter how long your drunk can go without food."); int exhaustion = NumExhaustion(); int drunkGuyX, drunkGuyY; drunkGuyX = columns / 2; drunkGuyY = rows / 2; char[,] island = Dimensions(columns, rows); Water(island, columns, rows); //Generate water. Bridge(island, columns, rows, bridgeCount); //Generate bridges. Land(island, columns, rows); //Generate land. island[drunkGuyX, drunkGuyY] = 'D'; //Generates drunks location. MoveDrunk(island, columns, rows, exhaustion, drunkGuyX, drunkGuyY, lives, drowns, starves); //Move D. Island(island, columns, rows); //Create the island's dimensions. Console.Read(); } // Generate the dimensions from user input. public static char[,] Dimensions(int columns, int rows) { char[,] island = new char[columns, rows]; return island; } // Create array. public static void Island(char[,] island, int x, int y) { for (int i = 0; i < x; i++) { for (int z = 0; z < y; z++) { Console.Write(island[i, z]); } Console.WriteLine(); } } // Fill land in array. public static char[,] Land(char[,] landArea, int x, int y) { for (int i = 1; i < x - 1; i++) { for (int z = 1; z < y - 1; z++) { landArea[i, z] = '+'; } } return landArea; } // Fill water in array. public static char[,] Water(char[,] landArea, int x, int y) { for (int i = 0; i < x; i++) { for (int z = 0; z < y; z++) { landArea[i, z] = 'W'; } } return landArea; } // Generate random placement of bridges from user input. public static char[,] Bridge(char[,] bridge, int x, int y, int bridgeCount) { int numBridges = 0; while (numBridges < bridgeCount) { Random random = new Random(); int randy = random.Next(0, 4); if (randy == 0) { int left = random.Next(1, x - 1); if (bridge[left, 0] != 'B') { bridge[left, 0] = 'B'; numBridges++; } } if (randy == 1) { int bottom = random.Next(1, y - 1); if (bridge[x - 1, bottom] != 'B') { bridge[x - 1, bottom] = 'B'; numBridges++; } } if (randy == 2) { int right = random.Next(1, x - 1); if (bridge[right, y - 1] != 'B') { bridge[right, y - 1] = 'B'; numBridges++; } } if (randy == 3) { int top = random.Next(1, y - 1); if (bridge[0, top] != 'B') { bridge[0, top] = 'B'; numBridges++; } } } return bridge; } // Move the D and update lives, drowns, and starves. public static char[,] MoveDrunk(char[,] drunk, int x, int y, int exhaustion, int drunkGuyX, int drunkGuyY, int lives, int drowns, int starves) { Random random = new Random(); int s = random.Next(1, 4); int gameCount = 0; int exh = 0; while (gameCount < 50) { if (exh < exhaustion) { if (s == 1)// Random 1, move up. { drunk[drunkGuyX, drunkGuyY] = '+'; exh++; Console.WriteLine("Your drunk moved, " + s + " and your exhaustion level is, " + exh + "."); drunkGuyY++; if (drunk[drunkGuyX, drunkGuyY] == 'W') { drowns++; gameCount++; Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n"); Play(lives, drowns, starves); } if (drunk[drunkGuyX, drunkGuyY] == 'B') { lives++; gameCount++; Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n"); Play(lives, drowns, starves); } drunk[drunkGuyX, drunkGuyY] = 'D'; random = new Random(); Island(drunk, x, y); //Console.ReadKey(); return MoveDrunk(drunk, x, y, exhaustion, drunkGuyX, drunkGuyY, lives, drowns, starves); } else if (s == 2)// Random 2, move right. { drunk[drunkGuyX, drunkGuyY] = '+'; exh++; Console.WriteLine("Your drunk moved, " + s + " and your exhaustion level is, " + exh + "."); drunkGuyX++; if (drunk[drunkGuyX, drunkGuyY] == 'B') { lives++; gameCount++; Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n"); Play(lives, drowns, starves); } if (drunk[drunkGuyX, drunkGuyY] == 'W') { drowns++; gameCount++; Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n"); Play(lives, drowns, starves); } drunk[drunkGuyX, drunkGuyY] = 'D'; random = new Random(); Island(drunk, x, y); //Console.ReadKey(); return MoveDrunk(drunk, x, y, exhaustion, drunkGuyX, drunkGuyY, lives, drowns, starves); } else if (s == 3)// Random 3, move down. { drunk[drunkGuyX, drunkGuyY] = '+'; exh++; Console.WriteLine("Your drunk moved, " + s + " and your exhaustion level is, " + exh + "."); drunkGuyY--; if (drunk[drunkGuyX, drunkGuyY] == 'B') { lives++; gameCount++; Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n"); Play(lives, drowns, starves); } if (drunk[drunkGuyX, drunkGuyY] == 'W') { drowns++; gameCount++; Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n"); Play(lives, drowns, starves); } drunk[drunkGuyX, drunkGuyY] = 'D'; random = new Random(); Island(drunk, x, y); //Console.ReadKey(); return MoveDrunk(drunk, x, y, exhaustion, drunkGuyX, drunkGuyY, lives, drowns, starves); } else// Random 4, move left. { drunk[drunkGuyX, drunkGuyY] = '+'; exh++; Console.WriteLine("Your drunk moved, " + s + " and your exhaustion level is, " + exh + "."); drunkGuyX--; if (drunk[drunkGuyX, drunkGuyY] == 'B') { lives++; gameCount++; Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n"); Play(lives, drowns, starves); } if (drunk[drunkGuyX, drunkGuyY] == 'W') { drowns++; gameCount++; Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n"); Play(lives, drowns, starves); } drunk[drunkGuyX, drunkGuyY] = 'D'; random = new Random(); Island(drunk, x, y); //Console.ReadKey(); return MoveDrunk(drunk, x, y, exhaustion, drunkGuyX, drunkGuyY, lives, drowns, starves); } } // End the game with input. if (exh == exhaustion) { starves++; gameCount++; Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n"); Console.WriteLine("Do you want to keep playing? Enter n to end the game and any other key to play again."); String inStringValue; inStringValue = Console.ReadLine(); string endGame = inStringValue; if (endGame == "n") { Console.WriteLine("This Game is about to close in 4 seconds..."); System.Threading.Thread.Sleep(4000); System.Environment.Exit(0); } else { Play(lives, drowns, starves); } } } return drunk; } } }
  13. Okay, well I thought that's what I needed to get this to work but sadly my drunk guy keeps making two turns, and my bridges on the right wont appear, and my end game method is dysfunctional? I tried the approach for adding another class but with the current state of my code I feel like its to difficult to rearrange everything. I did mange to make some progress but once I added my lives, drowns, and starves my code became inoperable. Here's what I have for anyone willing to give more suggestions.  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace IslandDrunk { class Program { static void Main(string[] args) { int lives = 0, drowns = 0, starves = 0; Console.WriteLine("All you need to know... \n1 moves drunk up, 2 moves drunk right, 3 moves drunk down, 4 moves drunk left"); Play(lives, drowns, starves); } public static void Play(int lives, int drowns, int starves) { int exhaustion = 0; Console.WriteLine("Please enter the amount of rows you want for your island, \nmust be greater than 3."); string input = Console.ReadLine(); int rows = int.Parse(input); Console.WriteLine("Please enter the amount of columns you want for your island, \nmust be greater than 3."); input = Console.ReadLine(); int columns = int.Parse(input); Console.WriteLine("\nPlease enter the number of bridges you prefer."); string bridgeInput = Console.ReadLine(); int bridgeCount = int.Parse(bridgeInput); Console.WriteLine("You are given " + (rows * columns) / 3 + " lives based on the size of your island. \n"); Random randyX = new Random(); Random randyY = new Random(); int drunkGuyX, drunkGuyY; drunkGuyX = randyX.Next(1, rows - 1); drunkGuyY = randyY.Next(1, columns - 1); char[,] island = Dimensions(rows, columns); Water(island, rows, columns); Bridge(island, rows, columns, bridgeCount); Land(island, rows, columns); island[rows / 2, columns / 2] = 'D'; MoveDrunk(island, rows, columns, lives, drowns, starves, exhaustion, drunkGuyX, drunkGuyY); Island(island, rows, columns); Console.Read(); } // Generate the dimensions from user input. public static char[,] Dimensions(int rows, int columns) { char[,] island = new char[rows, columns]; return island; } // Create array. public static void Island(char[,] island, int x, int y) { for (int i = 0; i < x; i++) { for (int z = 0; z < y; z++) { Console.Write(island[i, z]); } Console.WriteLine(); } } // Fill land in array. public static char[,] Land(char[,] landArea, int x, int y) { for (int i = 1; i < x-1; i++) { for (int z = 1; z < y-1; z++) { landArea[i, z] = '+'; } } return landArea; } // Fill water in array. public static char[,] Water(char[,] landArea, int x, int y) { for (int i = 0; i < x; i++) { for (int z = 0; z < y; z++) { landArea[i, z] = 'W'; } } return landArea; } // Generate random placement of bridges from user input. public static char[,] Bridge(char[,] bridge, int x, int y, int bridgeCount) { int numBridges = 0; while (numBridges < bridgeCount) { Random random = new Random(); int randy = random.Next(0, 4); if (randy == 0) { int left = random.Next(1, x-1); if (bridge[left, 0] != 'B') { bridge[left, 0] = 'B'; numBridges++; } } if (randy == 1) { int bottom = random.Next(1, y - 1); if (bridge[x - 1, bottom] != 'B') { bridge[x - 1, bottom] = 'B'; numBridges++; } } if (randy == 2) { int right = random.Next(1, x - 1); if (bridge[y - 1, right] != 'B') { bridge[y - 1, right] = 'B'; numBridges++; } } if (randy == 3) { int top = random.Next(1, y - 1); if (bridge[0, top] != 'B') { bridge[0, top] = 'B'; numBridges++; } } } return bridge; } public static char[,] MoveDrunk(char[,] drunk, int x, int y, int lives, int drowns, int starves, int exhaustion, int drunkGuyX, int drunkGuyY) { Random random = new Random(); int s = random.Next(1, 4); if (drunk[drunkGuyX, drunkGuyY] == 'B') { while (starves <= ((x * y) / 3)) { if (s == 1) { drunk[drunkGuyX, drunkGuyY] = '+'; drunk[drunkGuyX, drunkGuyY + 1] = 'D'; exhaustion++; Console.WriteLine("Your drunk moved, " + s + " and your exhaustion level is, " + exhaustion + "."); random = new Random(); Island(drunk, x, y); Console.Read(); return MoveDrunk(drunk, x, y, lives, drowns, starves, exhaustion, drunkGuyX, drunkGuyY); } else if (s == 2) { drunk[drunkGuyX, drunkGuyY] = '+'; drunk[drunkGuyX + 1, drunkGuyY] = 'D'; exhaustion++; Console.WriteLine("Your drunk moved, " + s + " and your exaustion level is, " + exhaustion + "."); random = new Random(); Island(drunk, x, y); Console.Read(); return MoveDrunk(drunk, x, y, lives, drowns, starves, exhaustion, drunkGuyX, drunkGuyY); } else if (s == 3) { drunk[drunkGuyX, drunkGuyY] = '+'; drunk[drunkGuyX, drunkGuyY - 1] = 'D'; exhaustion++; Console.WriteLine("Your drunk moved, " + s + " and your exaustion level is, " + exhaustion + "."); random = new Random(); Island(drunk, x, y); Console.Read(); return MoveDrunk(drunk, x, y, lives, drowns, starves, exhaustion, drunkGuyX, drunkGuyY); } else { drunk[drunkGuyX, drunkGuyY] = '+'; drunk[drunkGuyX - 1, drunkGuyY] = 'D'; exhaustion++; Console.WriteLine("Your drunk moved, " + s + " and your exaustion level is, " + exhaustion + "."); random = new Random(); Island(drunk, x, y); Console.Read(); return MoveDrunk(drunk, x, y, lives, drowns, starves, exhaustion, drunkGuyX, drunkGuyY); } } if (exhaustion == ((x * y) / 3)) { starves++; GameOver(); } lives++; } else if (drunk[drunkGuyX, drunkGuyY] == 'B') { drowns++; GameOver(); } return drunk; } //Ends the game when called. public static void GameOver() { Console.WriteLine("This game just ended, you've reached your maximum allowed exaustion. \n Press any key to continue"); Console.ReadKey(); DialogResult Result = MessageBox.Show("Lets play again!", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (Result == DialogResult.Yes) { //Play(); } else { System.Environment.Exit(0); } } } }
  14. Okay thanks for the help! That was what I needed to know for me to get through this. I did feel like I was overhinking it a tad but a few restless nights can do that to you. I appreciate how fast I got responses as well.
  15. Hi community, this is my first ever post, I read the guidelines so I searched as much as I could in the forums for this topic and couldn't find a post similar. So I am currently programming this island maze using two-dimensional arrays in C#. The purpose of the program is to ask the user for the size of the island (x rows, y columns) and then have a drunk guy placed near the center of the maze. Using Random method, 1-4, if 1 then the drunk guy moves up if 2 he moves left and etc... Using (+'s) char to represent the land, and (W's) to represent the water, and (B's) to represent the random placement of bridges where he will survive if he makes it to them. So far I have everything working properly except the placement of the drunk islander. I've been using x/2, y/2 to have the (D) or drunkin guy placed in the center of the matrices or island. When I try to have my MoveDrunk method move drunk[x, y] up, left, down, right, I have no variable to associate with that random placement of the drunk guy coordinate or index. All I have found was the IndexOf() and BinarySearch() System.Array methods. But those are only one dimensional methods. I then found out there's no way to evaluate a two dimensional index? Supposedly I must now create my own algorithm to calculate that drunk guys location so I could move him (some variable)+1 right (some variable)-1 left etc... Any ideas on how I could create this for my game or even another approach I could take? Here's what's in my main method so you get a better idea.   Thanks, Dustin Console.WriteLine("All you need to know... \n1 moves drunk up, 2 moves drunk right, 3 moves drunk down, 4 moves drunk left"); Console.WriteLine("Please enter the amount of rows you want for your island, \nmust be greater than 3."); string input = Console.ReadLine(); int rows = int.Parse(input); Console.WriteLine("Please enter the amount of columns you want for your island, \nmust be greater than 3."); input = Console.ReadLine(); int columns = int.Parse(input); int exaustion = 0; //Console.WriteLine("\nPlease enter the number of bridges you prefer."); //string bridgeInput = Console.ReadLine(); //int bridgeCount = int.Parse(bridgeInput);   char[,] island = Dimensions(rows, columns); Water(island, rows, columns); Bridge(island, rows, columns);   //, bridgeCount Land(island, rows, columns); //InitDrunk(island, rows, columns); //Not using currently //PlaceDrunk(island, rows, columns); //Not using currently island[rows / 2, columns / 2] = 'D'; //Heres where I get my center placement of my drunk before passing into MoveDrunk() MoveDrunk(island, rows, columns, exhaustion); //<-- Heres where I got to find a way to get that index Island(island, rows, columns);   Console.Read();    
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!