package semBreak;
import java.io.*;
public class YLingaw {
	public static void main (String [] args) throws IOException {
		BufferedReader jill = new BufferedReader (new InputStreamReader (System.in));
		char[][] board = {	{' ','1','2','3',' '},
							{'1',' ',' ',' ','1'},
							{'2',' ',' ',' ','2'},
							{'3',' ',' ',' ','3'},
							{' ','1','2','3',' '}};
		YLingaw.ehem(board);
		int as = 3, o = 3;
		boolean team = true; boolean win = false;
		do{
			if(team){
				boolean empty = true;
				do{
					System.out.print("*: ");
					String move = jill.readLine();
					int a = Integer.parseInt(String.valueOf(move.charAt(0)));
					int b = Integer.parseInt(String.valueOf(move.charAt(1)));
					if(board[a][b] == ' '){
						board[a][b] = '*';
					}
					else{
						empty = false;
					}
					
				}while(!empty);
				as--;
				team = false;
			}
			else{
				boolean empty = true;
				do{
					System.out.print("o: ");
					String move = jill.readLine();
					int a = Integer.parseInt(String.valueOf(move.charAt(0)));
					int b = Integer.parseInt(String.valueOf(move.charAt(1)));
					if(board[a][b] == ' '){
						board[a][b] = 'o';
					}
					else{
						empty = false;
					}
				}while(!empty);

				team = true;
				o--;
			}
			YLingaw.ehem(board);
			
		}while(as >= 0 && o > 0);
		boolean done = false;
		do{
			if(team){
				boolean ok = false;
				do{
					System.out.print("*: ");
					String move = jill.readLine();
					int a = Integer.parseInt(String.valueOf(move.charAt(0)));
					int b = Integer.parseInt(String.valueOf(move.charAt(1)));
					int c = Integer.parseInt((String.valueOf(move.charAt(3))));
					int d = Integer.parseInt(String.valueOf(move.charAt(4)));
					if(board[a][b] == '*' && board[c][d] == ' '){
						if((a-c==1 || c-a==1) && b==d){
							board[a][b] = ' '; board[c][d] = '*';
							ok = true;
						}
						else if(a==c && (b-d==1||d-b==1)){
							board[a][b] = ' '; board[c][d] = '*';
							ok = true;
						}
						else if((a==b&&c==d) &&((a-c==1||c-a==1)) &&(b-d==1||d-b==1)){
							board[a][b] = ' '; board[c][d] = '*';
							ok = true;
						}
						else if(a==b&&(((a-c==1||c-a==1)&&b==d)||(b-d==1||d-b==1&&(a==c)))){
							board[a][b] = ' '; board[c][d] = '*';
							ok = true;
						}
					}
					else{
						ok = false;
					}
				}while(!ok);
				team = false;
			}
			else{
				boolean ok = false;
				do{
					System.out.print("o: ");
					String move = jill.readLine();
					int a = Integer.parseInt(String.valueOf(move.charAt(0)));
					int b = Integer.parseInt(String.valueOf(move.charAt(1)));
					int c = Integer.parseInt((String.valueOf(move.charAt(3))));
					int d = Integer.parseInt(String.valueOf(move.charAt(4)));
					if(board[a][b] == 'o' && board[c][d] == ' '){
						if((a-c==1 || c-a==1) && b==d){
							board[a][b] = ' '; board[c][d] = 'o';
							ok = true;
						}
						else if(a==c && (b-d==1||d-b==1)){
							board[a][b] = ' '; board[c][d] = 'o';
							ok = true;
						}
						else if((a==b&&c==d) &&((a-c==1||c-a==1)) &&(b-d==1||d-b==1)){
							board[a][b] = ' '; board[c][d] = 'o';
							ok = true;
						}
						else if(a==b&&(((a-c==1||c-a==1)&&b==d)||(b-d==1||d-b==1&&(a==c)))){
							board[a][b] = ' '; board[c][d] = 'o';
							ok = true;
						}
					}
					else{
						ok = false;
					}
				}while(!ok);
				team = true;
			}
			YLingaw.ehem(board);
			
			main:for(int x =1; x <= 3; x++){
				int same = 0;
				for(int y = 1; y <= 3; y++){
					if(board[x][y] == '*'){
						same++;
					}
				}
				if(same == 3){
					done = true;
					win = true;
					break main;
				}
			}
			if(!done){
				main:for(int x = 1; x <= 3; x++){
					int same = 0;
					for(int y = 1; y <= 3; y++){
						if(board[y][x] == '*'){
							same++;
						}
					}
					if(same == 3){
						done = true;
						win = true;
						break main;
					}
				}
			}
			if(!done){
				int same = 0;
				for(int x = 1; x <= 3; x++){
					if(board[x][x] == '*'){
						same++;
					}
				}
				if(same == 3){
					done = true;
					win = true;
				}
			}
			if(!done){
				int same = 0;
				for(int x = 1; x <= 3; x++){
					int y = 4-x;
					if(board[x][y] == '*'){
						same++;
					}
				}
				if(same == 3){
					done = true;
					win = true;
				}
			}
			if(!done){
			main:for(int x =1; x <= 3; x++){
				int same = 0;
				for(int y = 1; y <= 3; y++){
					if(board[x][y] == 'o'){
						same++;
					}
				}
				if(same == 3){
					done = true;
					win = false;
					break main;
				}
			}
			if(!done){
				main:for(int x = 1; x <= 3; x++){
					int same = 0;
					for(int y = 1; y <= 3; y++){
						if(board[y][x] == 'o'){
							same++;
						}
					}
					if(same == 3){
						done = true;
						win = false;
						break main;
					}
				}
			}
			if(!done){
				int same = 0;
				for(int x = 1; x <= 3; x++){
					if(board[x][x] == 'o'){
						same++;
					}
				}
				if(same == 3){
					done = true;
					win = false;
				}
			}
			if(!done){
				int same = 0;
				for(int x = 1; x <= 3; x++){
					int y = 4-x;
					if(board[x][y] == 'o'){
						same++;
					}
				}
				if(same == 3){
					done = true;
					win = false;
				}
			}
			}
		}while(!done);
		if(win){
			System.out.println("Congratulations! * wins!");
		}
		else{
			System.out.println("Congratulations! o wins!");
		}
		
	}
	public static void ehem(char[][]a){
		for(int x = 0; x < 5; x++){
			for(int y = 0; y < 5; y++){
				System.out.print(a[x][y] + " ");
			}
			System.out.println();
		}
	}
}

This is a simple console game. At first three moves you just input the asked ordinate base on the ordinates drawn in the board. The after the first three you need to move the stones you put. To move: input first the stone you want to move and then enter the ordinate where you want to move. My problem is that i made a condition to win. It works if your stones are straight in first column. But if you had straight in second or third there will be interruption. Please help me.

Are you seriously using goto (or something similar using labels)? That is the worst thing you can ever do in java.

I suggest the following:
- Lose any labels and goto you have (I think that is solvable by using more methods than you are now)
- Make more methods, so the code is clearer. Don't type three times the same thing.

When you have done that, please post your code, so I can help you further. This is unreadable, and not functional. If you make any program using labels like that, people will laugh at your code.

I'm sorry for this I'm just a beginner. I promise I will not do it again. I still not comfortable using more methods. Thanks for the reminder.

Are you seriously using goto (or something similar using labels)?

Actually, I'm using labels.

Even using label is not a proper way to do because the code is very inconsistence. You should construct your code to be properly nested or not nested loop.

// for example
if(!done){
  main:for(int x = 1; x <= 3; x++){
    int same = 0;
    for(int y = 1; y <= 3; y++){
      if(board[y][x] == 'o'){
        same++;
      }
    }
    if(same == 3){
      done = true;
      win = false;
      break main;
    }
  }
}

// could be
if(!done){
  boolean doneloop = false;
  for(int x = 1; x <= 3; x++) {
    int same = 0;
    for(int y = 1; y <= 3; y++) {
      if(board[y][x] == 'o') {
        same++;
      }
    }
    if(same == 3) {
      done = true;
      win = false;
      doneloop = true;
      break;
    }
    if (doneloop) {  // break out of the outer loop
      break;
    }
  }
}

Do you know how to use method (function)? That will make the code look cleaner and is easier to debug. The problem for me right now is that I don't know how to play YLingaw, so I don't know the winning rule you are talking about. Can you explain a simple rule of it? Is it the same as Tic Tac Toe?

PS: Your loop above may be changed or improved depends on how to play the game.

This program demonstrates one of the almost-acceptable uses of labels in Java - that is to label a loop then use that label in a break statement to make explicit the loop that is being broken out of. It can even be useful where you have nested loops and want to break out of one which is not the innermost. However, many people see this as a disguised goto and dislike it; almost everybody will agree it's bad Java style.

I'm sorry for this I'm just a beginner. I promise I will not do it again. I still not comfortable using more methods.

Don't be upset. It's typical of Daniweb that people say what they think, but it's your code they are criticising, not you.
Practice using methods to break up your code into manageable pieces - it's essential.

Of coure I'm not upset. Actually, I'm encouraged to learn to user methods. Thanks for the advice guys!

@Taywin: Yes, that somewhat like a TIC-TAC-TOE game but you have only the maximum of three stones and you just need to move any of those to create a straight line in order to win.

I'm sorry for using my local dialect as my class name.

Great, then this makes things a lot easier. If the board size will never be changed, you could use hard-coded for certain parts. You could simply check for a winning row, column, or diagonal direction. You would need only 1 loop if the size of the board is fixed.

Try to replace the code portion below in your all checking for winner part. However, if the board size can be changed, the code below need to be modified to be more dynamic. If you have any question, please feel free to ask. I hope I added enough comment + the code would explain itself.

// start checking
// check for winning row
for (int x=1; x<=3; x++) {
  // same in a row regardless row #
  if (board[x][1]==board[x][2] && board[x][1]==board[x][3] &&
      board[x][1]!=' ') {  // winning
    if (board[x][1]=='*') {
      win = true;
    }
    else {
      win = false;
    }
    done = true;
  }
}

// check for winning column if failed from row
if (!done) {
  for (int y=1; y<=3; y++) {
    // same in a column regardless column #
    if (board[1][y]==board[2][y] && board[1][y]==board[3][y] &&
        board[1][y]!=' ') {  // winning
      if (board[1][y]=='*') {
        win = true;
      }
      else {
        win = false;
      }
      done = true;
    }
  }
}

// check diagonal -- use hard-coded -- if not win from row or column
if (!done) {
  // same in a diagonal regardless direction
  if (((board[1][1]==board[2][2] && board[1][1]==board[3][3]) ||
       (board[3][1]==board[2][2] && board[3][1]==board[1][3])) &&
       board[2][2]!=' ')  {
    if (board[1][y]=='*') {
      win = true;
    }
    else {
      win = false;
    }
    done = true;
  }
}

// display winner if done
if (done) {
  ...
}

Edited: I forgot to check for ' ' as well >_<. Just edited.

One small suggestion:

if ( boolean_expression ) {
   variable = true
} else {
   variable = false;
}

is long way to write

variable = boolean_expression
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.