Just thought Tic Tac Toe and the Peg game are pretty similiar to what you're trying to do. You can use the same idea to pick positions on the board.
Tic Tac Toe
-This was before I learned scanner & a lot of other things since it's from my first few weeks of Java.
import java.io.*;
import java.util.*;
public class TicTacToe {
public static String GetInput() {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String i = "";
try {
i = reader.readLine();
}
catch (Exception e) {
System.out.println("Error with input");
}
return i;
}
public static void initialboard(String board[][]) {
int c = 0;
int e = 0;
//Print out '?' where there are blanks
while(c<board.length){
while(e<board[c].length){
board[c][e]="?";
e=e+1;
}
c=c+1;
e=0;
}
//hardcode spots on the board where the numbers are(coordinates)
board[0][0] = " ";
board[0][1] = "1";
board[0][2] = "2";
board[0][3] = "3";
board[1][0] = "1";
board[2][0] = "2";
board[3][0] = "3";
}
public static void displayboard(String board[][]) {
int c = 0;
int e = 0;
//print out every spot in the array
while(c<board.length) {
while(e<board[c].length) {
System.out.print(board[c][e]+" ");
e=e+1;
}
e=0;
c=c+1;
System.out.print("\n");
}
}
public static boolean checkwinner(String board[][], String p1, String p2) {
//check every combination for a winner and print out if they win
if(board[1][1]=="X"&&board[1][2]=="X"&&board[1][3]=="X") {
System.out.println(p1 + " WINS !!!");
return true;
}
else if(board[1][1]=="O"&&board[1][2]=="O"&&board[1][3]=="O") {
System.out.println(p2 + " WINS !!!");
return true;
}
else if(board[1][1]=="X"&&board[2][1]=="X"&&board[3][1]=="X") {
System.out.println(p1 + " WINS !!!");
return true;
}
else if(board[1][1]=="O"&&board[2][1]=="O"&&board[3][1]=="O") {
System.out.println(p2 + " WINS !!!");
return true;
}
else if(board[2][1]=="X"&&board[2][2]=="X"&&board[2][3]=="X") {
System.out.println(p1 + " WINS !!!");
return true;
}
else if(board[2][1]=="O"&&board[2][2]=="O"&&board[2][3]=="O") {
System.out.println(p2 + " WINS !!!");
return true;
}
else if(board[3][1]=="X"&&board[3][2]=="X"&&board[3][3]=="X") {
System.out.println(p1 + " WINS !!!");
return true;
}
else if(board[3][1]=="O"&&board[3][2]=="O"&&board[3][3]=="O") {
System.out.println(p2 + " WINS !!!");
return true;
}
else if(board[1][2]=="X"&&board[2][2]=="X"&&board[3][2]=="X") {
System.out.println(p1 + " WINS !!!");
return true;
}
else if(board[1][2]=="O"&&board[2][2]=="O"&&board[3][2]=="O") {
System.out.println(p2 + " WINS !!!");
return true;
}
else if(board[1][3]=="X"&&board[2][3]=="X"&&board[3][3]=="X") {
System.out.println(p1 + " WINS !!!");
return true;
}
else if(board[1][3]=="O"&&board[2][3]=="O"&&board[3][3]=="O") {
System.out.println(p2 + " WINS !!!");
return true;
}
else if(board[1][1]=="X"&&board[2][2]=="X"&&board[3][3]=="X") {
System.out.println(p1 + " WINS !!!");
return true;
}
else if(board[1][1]=="O"&&board[2][2]=="O"&&board[3][3]=="O") {
System.out.println(p2 + " WINS !!!");
return true;
}
else if(board[1][3]=="X"&&board[2][2]=="X"&&board[3][1]=="X") {
System.out.println(p1 + " WINS !!!");
return true;
}
else if(board[1][3]=="O"&&board[2][2]=="O"&&board[3][1]=="O") {
System.out.println(p2 + " WINS !!!");
return true;
}
else {
return false;
}
}
public static boolean checkfull(String board[][]) {
int c1 = 1;
int c2 = 1;
//check if there are any available spots on the board
while(c1<board.length) {
while(c2<board[c1].length) {
if(board[c1][c2].equals("?")) {
return false;
}
c2=c2+1;
}
c2=0;
c1=c1+1;
}
return true;
}
public static void main(String args[]) {
// declare variables
boolean winner = false;
String choice = "Yes";
String board[][] = new String[4][4];
String temp = " ";
int row = 0;
int column = 0;
String p1 = " ";
String p2 = " ";
String letter = "X";
String player = " ";
// play again loop
while (choice.compareToIgnoreCase("No")!=0&&choice.compareToIgnoreCase("N")!=0) {
// re-declare variables
winner = false;
row = 0;
column = 0;
letter = "X";
temp = " ";
initialboard(board);
player = " ";
System.out.println("Player 1 enter your name");
p1 = GetInput();
System.out.println("Player 2 enter your name");
p2 = GetInput();
// instrutions
System.out.println("Tic Tac Toe. Enter the row, then enter the column.");
// game loop
while(!winner) {
//change the board everytime a spot is changed
displayboard(board);
// Switch between player names depending on X or O
if(letter=="X") {
player = p1;
}
else {
player = p2;
}
// get input from user
System.out.println(player + " enter the row");
temp = GetInput();
row = Integer.parseInt(temp);
System.out.println(player + " enter the column");
temp = GetInput();
column = Integer.parseInt(temp);
// Switch between players and dont allow them to pick the same spot twice
if(board[row][column].equals("?")) {
board[row][column] = letter;
//check for winner
winner = checkwinner(board,p1,p2);
//check if board is full
if(checkfull(board)&&!winner){
System.out.println("Tie game");
break;
}
if(letter.equals("X")) {
letter = "O";
}
else {
letter = "X";
}
}
else {
System.out.println("Spot taken");
}
}
System.out.println("Would you like to play again?");
choice = GetInput();
if(choice.compareToIgnoreCase("Yes")==0) {
System.out.println("Yes was chosen");
}
else if(choice.compareToIgnoreCase("Y")==0) {
System.out.println("Yes was chosen");
}
else if(choice.compareToIgnoreCase("N")==0) {
System.out.println("No was chosen");
}
else if(choice.compareToIgnoreCase("No")==0) {
System.out.println("No was chosen");
}
else {
System.out.println("Invalid choice");
}
}
}
}
Peg Game
-I'm sure almost everyone has played this game, but if you haven't, look up the rules
import java.io.*;
import java.util.*;
public class PegGame {
public static String GetInput() {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String i = "";
try {
i = reader.readLine();
}
catch (Exception e) {
System.out.println("Error with input");
}
return i;
}
public static void initialboard(String board[][]) {
int c = 0;
int e = 0;
//prints out 'O' in any spot that isn't taken
while(c<board.length){
while(e<board[c].length){
board[c][e]="O";
e=e+1;
}
c=c+1;
e=0;
}
//numbers
board[0][1] = "1";
board[0][2] = "2";
board[0][3] = "3";
board[0][4] = "4";
board[0][5] = "5";
board[0][6] = "6";
board[0][7] = "7";
board[1][0] = "1";
board[2][0] = "2";
board[3][0] = "3";
board[4][0] = "4";
board[5][0] = "5";
board[6][0] = "6";
board[7][0] = "7";
//empty spots
board[0][0] = " ";
board[1][1] = "-";
board[1][2] = "-";
board[2][1] = "-";
board[2][2] = "-";
board[1][6] = "-";
board[1][7] = "-";
board[2][6] = "-";
board[2][7] = "-";
board[6][1] = "-";
board[6][2] = "-";
board[7][1] = "-";
board[7][2] = "-";
board[6][6] = "-";
board[6][7] = "-";
board[7][6] = "-";
board[7][7] = "-";
board[4][4] = " ";
}
public static void displayboard(String board[][]) {
//prints out all the spots in the array
int c = 0;
int e = 0;
while(c<board.length) {
while(e<board[c].length) {
System.out.print(board[c][e]+" ");
e=e+1;
}
e=0;
c=c+1;
System.out.print("\n");
}
}
public static boolean checkmoves(String board[][]) {
//row = r, column = c
int r = 1;
int c = 1;
//if there are no moves, it returns true
//checks right and down
while(r<board.length-2){
while(c<board.length-2) {
if(board[r][c].equals("O")&&board[r+1][c].equals("O")&&board[r+2][c].equals(" ")) {
return true;
}
if(board[r][c].equals("O")&&board[r][c+1].equals("O")&&board[r][c+2].equals(" ")) {
return true;
}
c=c+1;
}
c=0;
r=r+1;
}
r=board.length-1;
c=board.length-1;
//checks left and up
while(r>2){
while(c>2) {
if(board[r][c].equals("O")&&board[r-1][c].equals("O")&&board[r-2][c].equals(" ")) {
return true;
}
if(board[r][c].equals("O")&&board[r][c-1].equals("O")&&board[r][c-2].equals(" ")) {
return true;
}
c=c-1;
}
c=board.length-1;
r=r-1;
}
//return false if there is at least one move left
return false;
}
public static int checkpegs(String board[][], int pegs) {
//row = r, column = c
int r = 1;
int c = 1;
//checks the entire array for any 'O'
while(r<board.length){
while(c<board.length) {
if(board[r][c].equals("O")) {
pegs = pegs + 1;
}
c=c+1;
}
c=0;
r=r+1;
}
return pegs;
//looks for all the remaining pegs in the board and returns it
}
public static void main(String args[]) {
//declare variables
String board[][] = new String[8][8];
String choice = "Yes";
boolean winner = false;
int sr = 0;
int sc = 0;
int er = 0;
int ec = 0;
int score = 10;
int pegs = 0;
//play again loop
while (choice.compareToIgnoreCase("No")!=0&&choice.compareToIgnoreCase("N")!=0) {
//re-declare variables
winner = false;
pegs = 0;
score = 10;
System.out.println("Peg Game. 'O' are pegs and '-' are unusable");
System.out.println("Every peg leftover at the end of the game is -1 point from 10.");
System.out.println();
initialboard(board);
//game loop
while(!winner) {
//re-displays board everytime there is input
displayboard(board);
//calls checkmoves function to see if there's any moves left
if(!checkmoves(board)) {
pegs = checkpegs(board, pegs);
score = score - pegs;
System.out.println("No more moves, your score is " + score);
//call checkpegs function when no more moves to count the remaining pegs
break;
}
// get input and change it into int's so you can put it into the board array
// ask for a start and end point
System.out.print("Enter the start row ");
sr = Integer.parseInt(GetInput());
System.out.print("Enter the start column ");
sc = Integer.parseInt(GetInput());
System.out.print("Enter the end row ");
er = Integer.parseInt(GetInput());
System.out.print("Enter the end column ");
ec = Integer.parseInt(GetInput());
// left right movements
if(sr==er&&!board[sr][sc].equals("-")&&!board[er][ec].equals("-")) {
if(sc>ec) {
if(board[er][ec].equals(" ")&&board[sr][sc].equals("O")&&board[sr][sc-1].equals("O")) {
//left
board[er][ec] = "O";
board[sr][sc] = " ";
board[sr][sc-1] = " ";
}
else {
System.out.println("Invalid move");
}
}
else{
//right
board[er][ec] = "O";
board[sr][sc] = " ";
board[sr][sc+1] = " ";
}
}
// up down movements
else if(sc==ec&&!board[sr][sc].equals("-")&&!board[er][ec].equals("-")) {
if(sr>er) {
if(board[er][ec].equals(" ")&&board[sr][sc].equals("O")&&board[sr-1][sc].equals("O")) {
//down
board[er][ec] = "O";
board[sr][sc] = " ";
board[sr-1][sc] = " ";
}
else {
System.out.println("Invalid move");
}
}
else {
//up
board[er][ec] = "O";
board[sr][sc] = " ";
board[sr+1][sc] = " ";
}
}
}
System.out.println("Would you like to play again?");
choice = GetInput();
if(choice.compareToIgnoreCase("Yes")==0) {
System.out.println("Yes was chosen");
}
else if(choice.compareToIgnoreCase("Y")==0) {
System.out.println("Yes was chosen");
}
else if(choice.compareToIgnoreCase("N")==0) {
System.out.println("No was chosen");
}
else if(choice.compareToIgnoreCase("No")==0) {
System.out.println("No was chosen");
}
else {
System.out.println("Invalid choice");
}
}
}
}
Ask about anything in the code you want and good luck!