package rowgame.test;
import java.util.Scanner;
/**
 *
 * @author USER
 */
public class RAWGAMETEST {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
  
Scanner scanner=new Scanner(System.in);   
String pro="Please enter the number of spaces for the game (1-40): ";
int space=promptNumberReadLine(scanner,pro,40);
int[] game=new int[space];
System.out.println(rowToString(game));
int count=0;
int p1;
int p2;
String pp1="X, please enter your move (1-"+space+"): ";
String pp2="O, please enter your move (1-"+space+"): ";
int win=0;
while(win==0){
if(count%2==0){
p1=promptNumberReadLine(scanner,pp1,space);
while(!makeMove(game,1,p1)){   
System.out.println("That space is not available! Please try again.");
p1=promptNumberReadLine(scanner,pp1,space);
}
System.out.println(rowToString(game));
}else{
p2=promptNumberReadLine(scanner,pp2,space);
while(!makeMove(game,2,p2)){
System.out.println("That space is not available! Please try again.");
p2=promptNumberReadLine(scanner,pp2,space);
}
System.out.println(rowToString(game));
}  
count++;
win=winner(game);
}
if(win==1){
System.out.println("Game over! X wins.");
}else if(win==2){
System.out.println("Game over! O wins.");
}else if (win==-1){  
System.out.println("Game over! Tie game!");
}
}
public static int promptNumberReadLine(Scanner s, String prompt, int max){
int num=1;
String n="";
do {
if(num < 1||num>max){
System.out.println("That was not a valid number! Please try again.");   
}
System.out.print(prompt);
while (!s.hasNextInt()) {
s.nextLine();
System.out.println("That was not a valid number! Please try again.");
System.out.print(prompt);
}   
n= s.nextLine();
while(!isNumber(n)){
System.out.println("That was not a valid number! Please try again.");
System.out.print(prompt);
n= s.nextLine();
}
int result = Integer.parseInt(n);
num =result;
}while (num < 1||num>max);
return num;
}
public static boolean isNumber(String str) {
int size = str.length();
for (int i = 0; i < size; i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return size > 0;
}
public static String rowToString(int[] game){
String row="";
for(int i=0;i<game.length;i++){
if(game[i]==0){   
row+=".";
}else if(game[i]==1){
row+="X";
}else if(game[i]==2){
row+="O";   
}else{
row+="-";
}
}
return row;
}
public static int winner(int[] game){
int p1=0;
int p2=0;
int em=0;
for(int i=0;i<game.length;i++){
if(game[i]==1){
p1++;
} else if(game[i]==2){   
p2++;
} else if (game[i]==0){   
em++;
}   
}
if(em==0){
if(p1>p2){
return 1;
}else if(p1<p2){
return 2;
}else{
return -1;
}
}else{
return 0;
}
}
public static boolean makeMove(int[] game, int player, int position){
for(int i=0;i<game.length;i++){
if(game[i]==3){
game[i]=0;
}
}
boolean move=true;
if(position>game.length||position<1){
return false;
}else{
if(game[position-1]==0){
game[position-1]=player;
if(position==2){
if(game[0]!=player&&game[0]!=0&&game[0]!=3){
game[0]=3;
}
}else if(position==game.length-1){
if(game[game.length-1]!=player&&game[game.length-1]!=0&&game[game.length-1]!=3){
game[game.length-1]=3;
}
}
int count=0;
for(int i=0;i<game.length;i++){
if(game[i]==1){
for(int j=i+1;j<game.length-1;j++){
if(game[j]==2){
if(game[j+1]==1){
game[j]=3;
}
}
}
}
}
for(int i=0;i<game.length;i++){
if(game[i]==2){
for(int j=i+1;j<game.length-1;j++){
if(game[j]==1){
if(game[j+1]==2){
game[j]=3;
}
}
}
}  
}   
return true;
}else{
return false;
}
}
}
}