Jump to content

Help in programming a game of tic tac toe

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Stilmatz

Stilmatz

    Newbie

  • Members
  • Pip
  • 3 posts
Hi,

i need to solve this problem:
private String enterMoveIntoColPosition(String row, int colNum, String playerSymbol)
This method has three parameters, a String representing one row of the TicTacToe game (6 characters
in length, e.g., "X – O "), the column number (1, 2 or 3), and the player symbol (either "X" or "O").
The player symbol is inserted into the correct position of the row and the resulting String is returned by the method

The followig code was provided:
private String enterMoveIntoColPosition(String row, int colNum, String playerSymbol) {
String col1, col2, col3;

any help would be much appreciated!

Thanks

#2
CallinWire

CallinWire

    Newbie

  • Members
  • Pip
  • 8 posts
I'd probably use chars instead of Strings, but it sounds like that's not your call. Anyway, it looks like you have to use substrings to take the parts of "row" that won't change, and stick your new playerSymbol in it - resulting in a new string.

So if colNum = 2, you need to take the first 2 letters of row (symbol and a space), add the player symbol, then add the final three letters of row (space, symbol, space).

private String enterMoveIntoColPosition(String row, int colNum, String playerSymbol) {
		
	if (colNum == 2) {
		return row.substring(0,2) + playerSymbol + row.substring(3,6);
	}
	// fill in the rest ;)
}