Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Kent77

macrumors newbie
Original poster
Jan 13, 2009
9
0
I am almost done with my first sem of comp. sci. I'm in high school so we havn't done anything really complicated, well everything was complicated for me; I just didn't seem to get it.
For the final exam we have to make a program of our choice and have him approve of it.
I'm going to make a very simple tic tac toe game.

The board will look like this.

7 | 8 | 9
-----------
4 | 5 | 6
-----------
1 | 2 | 3

I want to ask the user whether or not s/he wants to play.
I am having problems with doing that, as well as figuring out how to replace the numbers with an X or O and printing it out every time; and then declaring a winner whenever they get three in a row.
The code can't be too long cause I have to retype it during my exam time.
I want it to be player vs player since I heard that it is easier.
I am really lost guys would someone be able to help me out?

Do I use boolean? How do I use boolean?
Do I use str.replace? How do I use it?
 

Kent77

macrumors newbie
Original poster
Jan 13, 2009
9
0
import java.util.Scanner;

public class ttt {
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
Scanner SC = new Scanner(System.in);
System.out.println(" Wanna play tic tac toe? Hit Y for yes, N for no.");
private int Y;
private int N;
SC.nextLine();
if (SC.nextLine() = Y) {
System.out.println(" Player 1 goes first, you are Xs. Type in the corresponding number to play your X.");

}

if (SC.nextLIne() = N) {
System.out.println(" Too bad, you're playing.");
}



System.out.println(" 7 | 8 | 9 ");
System.out.println("-----------");
System.out.println(" 4 | 5 | 6 ");
System.out.println("-----------");
System.out.println(" 1 | 2 | 3 ");



}

}

So many errors.
I'm going to wait till I get the first part done to do the rest.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I don't mean to be rude, but were you taught Java in this course?
There are two Scanners over System.in. You only use one. Why?
Why are Y and N declared private? Their scope is main, they are not class or instance variables.
Why are Y and N declared int? You want to compare these to Strings, so they need to be declared as such and initialized to "Y" and "N". Alternately, you could just use literals in the comparisons later.
Scanner's getline() returns a String. You call it once, but discard the value returned. You need to store this value in a String for later comparison.
You are trying to assign Y and N to subsequent invocations of getline(), which is nonsense. For one == is the equivalence operator, and = is the assignment operator. However, Strings cannot be compared this way, you need to use String's equals method.

As for drawing the board, I'd just draw it again after each turn. That's probably better than trying to do a lot of console manipulation.

Hopefully this is helpful. You should be furious at your teacher if this is what has been taught to you.

-Lee
 

Kent77

macrumors newbie
Original poster
Jan 13, 2009
9
0
I'm pretty sure this is his first year of teaching and he wasn't the greatest.
Mainly went around the room helping those who already knew what was going on, than those who really need it.

Half of that stuff in my program was purely guessing.
You could say that I have no idea what's going on and that's why I'm not taking the AP course next sem but I do plan on taking a beginner's course in college; and trying to get a grip on it then.


I don't remember how to initialize Y, or N. I've tried a bunch of stuff but don't get it. And I really don't get what else you are saying. What would I use instead of SC.nextLine, nextString?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I am sorry you had a bad experience in this course. If existing knowledge of programming was not considered a requirement/prerequisite, they did you a serious disservice.

Back to the technical issues in your code... if you really want to try to make this work, you're going to have to read some documentation on your own. It seems like a lot of the issues in this program are related to the usage of java.lang.String, so start here:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

Look specifically at the equals method. You need to use this to compare two String objects.

In terms of basic language features, you may want to start here:
http://java.sun.com/docs/books/tutorial/java/index.html
and specifically look at the section on Strings for details like how to initialize Strings and compare them to one another:
http://java.sun.com/docs/books/tutorial/java/data/strings.html

In terms of using the Scanner and nextLine(), you can check out its documentation here:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#nextLine()

It returns a String. It is a fine method to use, you just need to do something with the String it returns. Likely store it, than use it like:
Code:
String input = SC.nextLine();
if("Y".equals(input.toUpperCase()) {
...
} else if("N".equals(input.toUpperCase()) {
...
} else {
...
}

Some things you'll need to add:
-A data structure to contain board state. A simple char array might work, simply storing a space, an X, or an O. It might be nice if you made this a 2D array,3x3, because it would be easier to check for winning states.
-A way to see if a board state is terminal, and if it is winning for either player.
-Input from the user for each move, and validation that a legal move is being made.
-A way to reset the board if the user wants to play again.

Is your final soon? We can certainly try to help you along, but there may not be enough time for this to all "click" for you. Also, trying to memorize code, then dump it onto paper during a test is a terrible method of assessing one's knowledge.

-Lee
 

Kent77

macrumors newbie
Original poster
Jan 13, 2009
9
0
I think you're right.
Tic tac toe is harder than I thought.
You guys have an idea on what I could program?
Something a lot simpler, but still is a lil bit challenging?
 

VPrime

macrumors 68000
Dec 19, 2008
1,722
86
London Ontario
What did your course cover?
GUI, or just console programs?

What topics did you guys learn? some thing simple yet challenging could be a text editor, with syntax highlighting, and fonts...

If you have not done any gui work, than maybe a console program that scans your hard drive (or a given directory) to find a specific file, or file type... and sort it.
This covers most intro to comp sci topics.. File systems, sorting, recursive algorithms.. etc. Plus this is all stuff in every java book and internet tutorial so you can piece it together with some google searches and the library.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I would try something like a simple cipher. Take text, and a passphrase, and just use the passphrase character by character to rotate the characters in the text. You would have an encoding and decoding mode, both would take text and a password. One would "add" the passphrase, the other would "subtract" it. It would require some looping, some functions, the use of modulus, and a few string methods. User interaction would be minimal, and input validation would be unneeded.

-Lee
 

Kent77

macrumors newbie
Original poster
Jan 13, 2009
9
0
algorithms
gui
cipher

Those I know we did not learn. haha
And Lee I'm a little confused by what you mean by rotating characters?

btw thanks for all the help guys
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
algorithms
Reading that this was not taught makes me want to slap your teacher across the face. You are not taking an intro computer science course if they did not teach this. I would call it a "guess-and-check computer programming lab" or something. An algorithm is the logical steps one would take to solve a problem. It is NOT the code you use to solve a problem. You implement an algorithm in code to solve a given problem. From Oxford:
noun
a process or set of rules to be followed in calculations or other problem-solving operations, esp. by a computer : a basic algorithm for division.

GUI is an acronym for Graphical User Interface. It describes one method of interacting with a computer program. The other most common is the Command Line Interface. There are others, like an auditory/verbal interface (like some automated phone systems, etc.), but for general interaction with a computer it's normally GUI (normally prounounced Gee-You-Eye or gooey) or CLI (always, in my experience, prounouned See-Ell-Eye). I doubt very seriously that this course taught you any of the methods in Java for generating a GUI, and since you were using scanLine(), etc. it seems like the course is geared towards a CLI.

Essentially this is an encoding. In the specific case i was talking about it was encoding some text based on some other text. The relevant definition from Oxford:
a secret or disguised way of writing; a code : he was writing cryptic notes in a cipher | the information may be given in cipher.
• a thing written in such a code.
• a key to such a code

Those I know we did not learn. haha
And Lee I'm a little confused by what you mean by rotating characters?

btw thanks for all the help guys

My suggestion was to use a system by which you take a word, say "cheese" and a passphrase, to keep it simple "be". You would "roll" the letters(using their ascii values, or just their position in the alphabet), like so:
c (3) +b (2) = 5 (e)
h (8) +e (5) = 13 (m)
e (5) +b (2) = 7 (g)
e (5) +e (5) = 10 (j)
s (19) +b (2) = 21 (u)
e (5)+e (5) = 10 (j)

so the result would be emgjuj. When something is over 26, you'd just subtract 26 (or use modulus) to get the value.

-Lee
 

Kent77

macrumors newbie
Original poster
Jan 13, 2009
9
0
\I would call it a "guess-and-check computer programming lab" or something.

-Lee

You're exactly right.

Ok so now I get what the program does. Would I have to assign A to 1, B to 2, or is there a shorter way?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
You're exactly right.

Ok so now I get what the program does. Would I have to assign A to 1, B to 2, or is there a shorter way?

There are a LOT of ways. I don't know what you're "supposed to know" at this point. You could just write a function that returns the value for each letter (after forcing it to upper case, i think that would make everything easier. you could just run toUpperCase() on the whole string read in). How that function works depends on what you know. You could have a big switch that just has 26 cases (you could have a special value 27 for anything non-alphabetic, and just treat that as a space in decoding, etc.). You could have a big series of if-then-else if that's all you know. You could also depend on "character math" and just subtract 'A' then add 1, etc.

That function would then be used for each character to get its weight, then you could have a "encode character" routine that takes two characters and returns the encoded result. That function would call the "value" function once for each character, then add the two numbers returned, mod the result (% is the modules operator) by 26 or 27, then have another function (or just an array, perhaps) that returns the corresponding character.

You'd then just need to roll through both strings, encoding one character at a time and appending it to the result string (this doesn't perform well as Strings cannot be changed, only new Strings can be created, but using StringBuilder at this point is probably too advanced).

This has 4 or so functions, does a little I/O but doesn't really require input checking (maybe for choosing the function, encode vs. decode), and performs a (sort of) useful function. It's easy to test results by hand, and once you have both functions (encoding and decoding) done, you can check them against one another as well as by hand.

-Lee
 

JimmyDThing

macrumors regular
Aug 23, 2007
208
0
Kent, I've seen a few threads started by you looking for "help" with your projects.


I understand you are new to programming and that's all fair and great, but what you will learn if you stick with it is that you're "questions" are unanswerable because there are infinite ways to do things.

You still have a a high school mentality as to what college is all about. College isn't supposed to teach you specific skills (at least that's not it's end goal in the arts and sciences... business maybe)... the goal is to learn HOW to learn... on your own.

You need to come up with a clearer idea of what you want to do... try to accomplish it the best you can and when you're not sure how to do something... google it. I know that seems like a dumb solution, but the truth is anything you're doing has more than likely been done by someone else.

I don't mean you can google "java tic tac toe program" and come up with what you're looking for... I mean when you're not sure how to ask a user to enter "Y" or "N" for example, someone has done that and you can look it up.

You can't ask for help with a whole program, only a specific aspect of it... otherwise you'll spend all day explaining to the person helping you what you're trying to do.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.