Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


ThinkingsHard

Member Since 11 Sep 2012
Offline Last Active Dec 20 2014 07:19 PM

Posts I've Made

In Topic: Want to become a Computer Scientist

09 December 2014 - 07:18 AM

I recommend you work on creating some console games and then move on to creating some gui games. I took a class in C# this semester, because I previously had taken an intro to Java class, which had given me info on loops and arrays, one of our projects was to create a console game. I create minesweeper, where you just selected your position by typing it in. It was actually pretty fun to make.

 

The point is, is that you'll gain a lot more from hands on experience than from reading from the book, in my opinion. I've read sections of my textbook 10 times over and still don't know what I'm doing, I go to implement it,  it doesn't work. I get it working, I kinda understand better. Again this is all just personal opinion, but if you can create something simple from scratch, like a pong game, and you understand it perfectly well, it doesn't really take all that long. You could probably make the entire game in about 2-3 hours if you understood everything, and yet it took a group of us in class 2 weeks to get it working properly when we were still learning that stuff.

 

(Also in case this is understood as such, I'm not saying don't read the books, they are great, I'm saying to practice the stuff you traditionally see in the first 8 or so chapters of a programing book (Which is like, creating strings, ints, doubles, floating points, printing out statements, sending information to the program with input, arrays, loops, drawing shapes) because for me and my class, it's poor understanding of those first semester / second semester concepts that are causing issues, not the higher up database and networking stuff)


In Topic: Loop infinitely but wait before next loop

06 December 2014 - 03:16 PM

How I built this so far was I started, before I added gui elements, I had it at a simple console input is then sent to server, and then the server responds. I know the problem is in the code I linked. The "component.text()" is a reference to a JTextField. The class that holds the code I showed you, has two inner classes. Everything works except what I've mentioned about the input is stuck in an infinite loop. The code I linked is working code for A SINGLE interation. If I surround

while(connected)
{
			System.out.println("Type in a word to send to the server.");
			// Sends the word to the server from the TextField
			System.out.print("Sending: " + component.text() + "\n");
			out.println(component.text());
			out.flush();
			// gets the Response from the server
			response = in.nextLine();
			if(!response.equals("Word does not exist"))
			{

					wordList.add(response);
				System.out.println("Receiving: " + response + "\n");
				
			}
			for(String str: wordList)
			{
				System.out.println(wordCount);
				wordCount++;
			}
}
		

the stuff I WANT to do with a while statement, it keeps adding the last input to the ArrayList and keeps increasing wordCount endlessly. Basically.

 

For pseudo code.

 

I want to continue to listen for input from the user

Check it against the dictionary

if it's a real word, send it back from the server to the client

add that word to an ArrayList

increase the wordCount (really just there to determine if I've finally got it working so it will only add one word to the ArrayList for that input)

 

Repeat this process for the duration the game is running.

 

In the event that I am wrong about everything else working in my code, I will supply the entire 3 classes I am working with.

 

The Client (That controlls the words used):

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ControllerClient 
{
	static boolean connected = true;
	public static String response = "test";
	public static ClientComponent component;
	public static int wordCount = 0;
	public static String wordCountString = "";
	public static void main(String[] args) throws IOException
	{
		component = new ClientComponent();
		final int SBAP_PORT = 8888;
		//Creates a frame
		JFrame frame = new JFrame();
		frame.setSize(1080,720);
		frame.add(component);
		frame.setVisible(true);


		//creates a socket
		// with a input and output stream
		Socket s = new Socket("localhost", SBAP_PORT);
		InputStream instream = s.getInputStream();
		OutputStream outstream = s.getOutputStream();
		Scanner in = new Scanner(instream);
		PrintWriter out = new PrintWriter(outstream); 

		ArrayList<String> wordList = new ArrayList<String>();
		// Creates a scanner to read input from the keyboard
		Scanner input = new Scanner(System.in);
		// instantiates a string for use with the input from the user
		String command = "";

		// While connected is true, then prompt the user to send words to the server
			
	
			System.out.println("Type in a word to send to the server.");
			// Sends the word to the server from the TextField
			System.out.print("Sending: " + component.text() + "\n");
			out.println(component.text());
			out.flush();
			// gets the Response from the server
			response = in.nextLine();
			if(!response.equals("Word does not exist"))
			{

					wordList.add(response);
				System.out.println("Receiving: " + response + "\n");
				
			}
			for(String str: wordList)
			{
				System.out.println(wordCount);
				wordCount++;
			}
		

		s.close();

	}
}

class ClientComponent extends JComponent
{

	private Object setLayout;
	public JTextField controllerInput;
	public TextFieldKeyListener textListener;

	public ClientComponent()
	{

		// creates the TextField and sets it's bounds
		controllerInput = new JTextField();
		this.setLayout = null;
		textListener = new TextFieldKeyListener(this);
		controllerInput.setBounds(50, 480, 1000, 30);
		controllerInput.setText("Type here");
		this.add(controllerInput);
		controllerInput.setVisible(true);
		controllerInput.addKeyListener(textListener);

	}
	// draw method
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		g2.setFont(new Font("TimesRoman", Font.PLAIN, 15));
		g2.drawString(String.valueOf(ControllerClient.wordCount), 10, 10);
		g2.drawString(ControllerClient.response, 320, 30);
		//text();

	}

	public void update()
	{
		Graphics g = getGraphics();
		controllerInput.setText("");
		controllerInput.hasFocus();
		//paint(g);
		repaint();


	}
	public String text()
	{
		return textListener.getTextField();
	}


}

class TextFieldKeyListener implements KeyListener
{
	public String text;
	public ClientComponent gamePanel;
	public TextFieldKeyListener(ClientComponent gamePanel)
	{
		this.gamePanel = gamePanel;
	}
	@Override
	/**
	 * Gets the key press event for the text field
	 */
	public void keyPressed(KeyEvent event) 
	{
		// makes sure that the key is the enter key before actually working
		if(event.getKeyCode() == KeyEvent.VK_ENTER)
		{
			JTextField textField = (JTextField) event.getSource();
			text = textField.getText();

			gamePanel.update();
		}
		// if it's not, do nothing
		else
		{	
		}
	}
	@Override
	public void keyReleased(KeyEvent arg0) 
	{
	}
	@Override
	public void keyTyped(KeyEvent arg0) 
	{
	}
	//returns the text in the textfield
	public String getTextField()
	{
		return text;
	}

}


The Service (Runnable Thread):

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;



public class ControllerService implements Runnable
{
	private Socket s;
	private Scanner in;
	private PrintWriter out;
	private File aFile = new File("Dictionary.txt");

	public ControllerService(Socket aSocket)
	{
		s = aSocket;
	}

	public void run()
	{
		
			System.out.println("Has reached run method of ControllerService");      
			try
			{
				try
				{
					in = new Scanner(s.getInputStream());
					out = new PrintWriter(s.getOutputStream());
					while(true)
					{
					doService(); 
					}
				}
				finally
				{
					s.close();
				}
			}
			catch (IOException exception)
			{
				exception.printStackTrace();
			}
		
	}



	public void doService() throws IOException
	{      

		System.out.println("Has reached doService method of ControllerService");

		String command = in.next();
		command = command.toLowerCase();
		executeCommand(command);

	}


	public void executeCommand(String command) throws FileNotFoundException
	{
		System.out.println("In the executeCommand method");

		Scanner scanFile = new Scanner(new File("Dictionary.txt"));
		int val = 0;
		//While the value is not 2
		while(val != 2)
		{
			// Prints out a line to tell me it has reached the first loop of the method
			System.out.println("In the executeCommand first loop of the method");
			// while the Dictionary file has another line
			while(scanFile.hasNextLine())           
			{
				// Prints out to tell me it has reached the inner loop
				System.out.println("In the executeCommand second loop of the method");
				// Creates a string that is the word on that line of the dictionary file
				String line = scanFile.nextLine();
				// if line.indexOf command doesn't return -1(which is false, meaning
				// that it wasn't that word
				//Then it returned 1, which means it WAS that word
				if(line.indexOf(command) != -1)
				{
					// sends the word back to the client
					// and breaks out of the loop
					out.print(command +"\n");
					out.flush();
					val= 2;
					break;
				}
				// the value stays 1 if the above is not true
				else
				{
					val = 1;
				} 
			}
			// if the value is 1 after the loop, the the word does not exist in the file
			if(val == 1)
				out.print("Word does not exist\n");
			out.flush();
			val=2;
		}
	}
}

The Server:

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class GameServer 
{
	 public static void main(String[] args) throws IOException
	 {  
		 final int C_PORT = 8888;
		 
	      ServerSocket controller = new ServerSocket(C_PORT);
	      System.out.println("Waiting for clients...");
	      
	      Socket controllerPlayer = controller.accept();
	      System.out.println("Clients connected....");
	      ControllerService cService = new ControllerService(controllerPlayer);
	      Thread cPlayer = new Thread(cService);
	      int val = 1;
	     
	      cPlayer.start();
	      
	      
	 }
}


In Topic: Loop infinitely but wait before next loop

06 December 2014 - 12:21 PM

Yes. The program is a networked 3+ player game. I'm currently only working on one client, and that is a special client.

Before I get into that, the overall game is basically a 'word attack' Where words will fall from the top of the frame to the bottom of the players screens, and they have to correctly type the word in. They get points for doing so, and the words fall at different speeds depending on the size of the word, with smaller words falling faster than larger words. There is an overall time limit , and if none of the players lose all 3 of their lives (working on just 2 players fighting eachother for now), and they don't reach whatever point limit I create for winning, then lives get converted to points and the winner is determined on overall points.

The special client has a frame that will allow him/her to see the other players screens, and then a blank text field. This user controlls what words fall down the screen. Anything they type into that text field, is sent to a process that determines if that word is in the dictionary, and then it removes any upper case characters, replaces them so the entire word is lower case, and sends that word back from the server to the client(s).

 

I'm trying to get it so that when this special client submits a word, if it is a word that passed the screening of if it is or is not in the dictionary, then it is added to a list. This list will be used later. Right now, this list continues to get populated with the same word. So if your word was 'what'

You'd get a list like

what

what

what

what

what... etc.

 

Right now, I want it to take that, and make it so if you submit what, you get

what

*waits for next word*


In Topic: Beale Cipher

11 May 2014 - 06:17 PM

I fixed it by using this code

	public  void encrypt2(String str, String key)
	{

		for (int i = 0; i <  str.length();  i++) 
		{
			char baelleCh = str.charAt(i);
			char ch = key.charAt(i % key.length());
			// allows spaces
			if(baelleCh == ' ')
			{
				cryptedString2 += baelleCh;    
			}
			else
			{		
				// like the Ceasar Cipher, takes the char in the string, minus 'a' plus the numerical value of the character of the key, mod 26, plus 'a'
				char encoding = (char) (((baelleCh - 'a') + (ch-'a')) % 26 + 'a');
				cryptedString2 += encoding;
			}
		}
	}

In Topic: Beale Cipher

11 May 2014 - 03:51 AM

	public void encrypt2(String str, String key)
	{
		
		for (int i = 0; i <  str.length();  i++) 
			{
			
			char baelleCh = str.charAt(i);
			char ch = key.charAt(i % key.length());
			
			if(baelleCh == ' ')
			{
				cryptedString2 += baelleCh;	
			}
			
		    char encoding = (char) (((baelleCh - 'a') + (ch - 'a')) % 26 + 'a');
		    cryptedString2 += encoding;
			}

It works now, the only problem is, it adds characters like \ and _. Is there a way to make it not have these when it's encrypting the string? I can't really think of anything that would remove them without messing with the encryption. editting this to add that If I change it to

 

 char encoding = (char) (((baelleCh - 'a') + (Character.getNumericValue(ch)%26 + 'a')) % 26 + 'a');

 

I get a neater string with no odd symbols, however I then run into a problem of, I'm having it not align the words properly. There are spaces, and it's spaced out and looks nice, however there is a 3 letter encrypted string where a 2 letter string should be, same for a 4 and a few 6's. It's quite strange. I'm not sure why.


PARTNERS