[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 11 KB, 300x300, 0_746ac3fe2bf33c3a0a07b05a23c46e47.jpg [View same] [iqdb] [saucenao] [google]
5195456 No.5195456 [Reply] [Original]

1st year computer science major here. Any CS'ers wanna help me out? kinda stuck on some code involving loops

>> No.5195463

>>5195456

looping is one of the hardest concepts in computer science, so don't despair if you have trouble with them for the first year or two.

>> No.5195462
File: 223 KB, 670x429, 1349552142049.jpg [View same] [iqdb] [saucenao] [google]
5195462

Post code or ask question.

>> No.5195466

part 1:
public class P3TicTacToe
{
private String p1 = " ";
private String p2 = " ";
private String p3 = " ";
private String p4 = " ";
private String p5 = " ";
private String p6 = " ";
private String p7 = " ";
private String p8 = " ";
private String p9 = " ";

>> No.5195470

part2:
public P3TicTacToe()
{
p1 = "_";
p2 = "_";
p3 = "_";
p4 = "_";
p5 = "_";
p6 = "_";
p7 = "_";
p8 = "_";
p9 = "_";
}

>> No.5195471

>>5195463
>this is what CS majors actually believe

10/10

>> No.5195474

fucking flood detection
part 3:
public void drawBoard()
{
String position1 = p1 + "|";
System.out.println();
String position2 = p2 + "|";
System.out.println();
String position3 = p3 + "|";
System.out.println();
String position4 = p4 + "|";
System.out.println();
String position5 = p5 + "|";
System.out.println();
String position6 = p6 + "|";
System.out.println();
String position7 = p7 + "|";
System.out.println();
String position8 = p8 + "|";
System.out.println();
String position9 = p9 + "|";
System.out.println();
}

>> No.5195477

here is what im stuck on i dont know what to set the winning combos to?
part 4:
public boolean checkWin(String player)
{
if(p1.equals(player) && p2.equals(player) && p3.equals(player))
if(p4.equals(player) && p5.equals(player) && p6.equals(player))
if(p7.equals(player) && p8.equals(player) && p9.equals(player))
if(p1.equals(player) && p4.equals(player) && p7.equals(player))
if(p2.equals(player) && p5.equals(player) && p8.equals(player))
if(p3.equals(player) && p6.equals(player) && p9.equals(player))
if(p1.equals(player) && p5.equals(player) && p9.equals(player))
if(p9.equals(player) && p5.equals(player) && p7.equals(player))
}
}

>> No.5195485

>>5195466
p.s. its for a text based tic tac toe if youre incompetent

>> No.5195486
File: 19 KB, 350x272, HA_HA_HA_OH_WOW.jpg [View same] [iqdb] [saucenao] [google]
5195486

>>5195466
>>5195470
>>5195474
>>5195477
>what is an array

>> No.5195497

>>5195486
thanks. -_-

>> No.5195508

>>5195486

at my school arrays are grad student level, as well as advanced looping, like this guy's example.

>> No.5195516

>>5195474
With this you're just making 9 empty lines, not an actual board

>> No.5195529

theres an example of a tic tac toe board where the instance variables are:
private String [ ] [ ] board;
private static final int ROWS = 3;
private static final int COLUMNS = 3;
but,
the prof instruction are "Include 9 instance variables, one for each position of the tic tac toe board. These should be Strings, as they will eventually hold either “_”, “X” or “O”. Call them anything you like."

>> No.5195530

>>5195477
>using .equals with strings
look through your api dawg, it doesnt do what you think it does.

>> No.5195538

>>5195530
its not my if statement this is what the instructor wants:
"Knowing there are 8 win conditions, you can use your instance variable positions to check for a win. You’ll need to create a series of if/else if statements to check each possibility. I’ll give you the first, you’ll need to add the rest:

if(p1.equals(player) && p2.equals(player) && p3.equals(player)) { … }"
idk what the "rest" is...

>> No.5195535

>>5195477
You need some sort of statement after each if (blah blah) statement.
> if (blah blah) return true;

Otherwise it looks fine. I'd investigate using arrays in the future, though. You could have a constant winningCombo[8][3], and your check could loop over i and see if winningCombo[i][1] == [i][2] == [i][3]. It would just make nicer and easier to debug code.

>> No.5195540

>>5195477
>>5195474
>>5195470
>>5195466
you have got to be the biggest noob ever. Try making a board class which holds a 3x3 array of spaces. You can easily use methods to place x's or o's in the spaces and put everything in a loop to check if anyone won after each move.>>5195477 this in particular is especially retarded. Create 3 methods. One that checks if rows are complete, one for columns and one for the diagonals. You can also use these methods so the AI knows how to block you and connect 3 x's or o's. I would just post my code but I don't have it with me right now. tl;dr have fun in 1st year comp sci

>> No.5195547

>>5195530
The equals method should be okay in this case.

>> No.5195551

>>5195538
if(p1.equals(player) && p2.equals(player) && p3.equals(player)) return true;
else if (p4.equals(player) && p5.equals(player) && p6.equals(player)) return true;
...
else return false

>> No.5195559

>>5195547
yeah, realized I was thinking of something else after I posted.
Check to see if your method is returning any value at all in a debug main method.
You should set a variable equal to false at the beginning of the method then set it true if any if conditions are true. else if's will save you some computing time and it is good practice to use them but that isn't your problem.

>> No.5195562

>>5195540
yeah, so like what i just put?>>5195529
i know the last part is retarded

>> No.5195563

>>5195456

just curious OP, how did you wind up in CS if you know absolutely nothing about programming?

>> No.5195564

>>5195551
multiple return statements in a single method is bad practice but this should work.

>> No.5195573
File: 63 KB, 720x960, 313207_186481258098811_1327904441_n.jpg [View same] [iqdb] [saucenao] [google]
5195573

>>5195551
thank you

>> No.5195595

>>5195562
When I did this problem I made a method that put one of the computers marks in each of the locations at a time to check if the computer could connect 3. The computer also put the player's marks in each spot at a time to check if there are any locations that need to be blocked. This makes use of the methods that check if any 3 are connected.

>> No.5195713

Post your code on /g/ with code tags, or pastebin with Java syntax highlighting.

To do code tags, put

before your code, and

after your code.

>> No.5195728

>>5195563
We all started somewhere. No need to pick on him.

>>5195564
In this case, it also makes the code clearer than doing something like
> boolean b = false;
> b = b || (test1);
> b = b || (test2);
> ...
Also, depending upon the compiler, multiple return statements are probably more efficient (though processor architecture/branch prediction might influence this as well).

>> No.5195736

>>5195551
You actually don't need the elses in this code.