Below is my solution for Problem A: Tic-Tac-Toe-Tomek
/*
* @author niraj.nijju
*/
package gcj.TwelveThirteen;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class TicTacToeTomek {
public static void main(String [] args){
try{
FileInputStream fstream = new FileInputStream("A-small-practice.in");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
strLine = br.readLine();
int T = Integer.valueOf(strLine);
System.out.println("T: "+T);
int i=1;
while ((strLine = br.readLine()) != null) {
List<String> string = new ArrayList<String>();
string.add(strLine);
string.add(br.readLine());
string.add(br.readLine());
string.add(br.readLine());
if (T-- >0){
br.readLine();
}
int st = status(string, 1);
if(st==1)
System.out.println ("Case #"+i++ +": X won");
if(st==2)
System.out.println ("Case #"+i++ +": O won");
if(st==0)
System.out.println ("Case #"+i++ +": Draw");
if(st==3)
System.out.println ("Case #"+i++ +": Game has not completed");
//System.out.println (strLine);
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
public static int status(List<String> string, int t){
int status=0; // (1.X 2.O 0.draw 3.incomplete)
int i=0;
Boolean ic=false;
List<String> sl = new ArrayList<String>();
sl.add("");sl.add("");
sl.add("");sl.add("");
for(i=0;i<4;i++){
String s = string.get(i);
if(s.contains("."))
ic = true;
for(int z=0;z<4;z++){
sl.set(z, sl.get(z)+s.charAt(z));
}
if(s.equalsIgnoreCase("XXXX"))
return 1;
else if(s.equalsIgnoreCase("OOOO"))
return 2;
if(s.contains("T")){
int xCount=0, oCount=0;
for(int j=0;j<4;j++){
if( s.charAt(j) == 'X')
xCount++;
else if( s.charAt(j) == 'O')
oCount++;
// else if( s.charAt(j) == '.')
// ic = true;
}
if(xCount==3)
return 1;
if(oCount==3)
return 2;
}
}
if( (string.get(0).charAt(0)=='X' || string.get(0).charAt(0)=='T' )
&& (string.get(1).charAt(1)=='X' ||string.get(1).charAt(1)=='T')
&& (string.get(2).charAt(2)=='X' ||string.get(2).charAt(2)=='T')
&& (string.get(3).charAt(3)=='X' ||string.get(3).charAt(3)=='T') )
return 1;
if( (string.get(0).charAt(0)=='O' || string.get(0).charAt(0)=='T' )
&& (string.get(1).charAt(1)=='O' ||string.get(1).charAt(1)=='T')
&& (string.get(2).charAt(2)=='O' ||string.get(2).charAt(2)=='T')
&& (string.get(3).charAt(3)=='O' ||string.get(3).charAt(3)=='T') )
return 2;
if( (string.get(3-0).charAt(0)=='X' || string.get(3-0).charAt(0)=='T' )
&& (string.get(3-1).charAt(1)=='X' ||string.get(3-1).charAt(1)=='T')
&& (string.get(3-2).charAt(2)=='X' ||string.get(3-2).charAt(2)=='T')
&& (string.get(3-3).charAt(3)=='X' ||string.get(3-3).charAt(3)=='T') )
return 1;
if( (string.get(3-0).charAt(0)=='O' || string.get(0).charAt(3-0)=='T' )
&& (string.get(3-1).charAt(1)=='O' ||string.get(3-1).charAt(1)=='T')
&& (string.get(3-2).charAt(2)=='O' ||string.get(3-2).charAt(2)=='T')
&& (string.get(3-3).charAt(3)=='O' ||string.get(3-3).charAt(3)=='T') )
return 2;
if(t==1){
return status(sl,2);
}
if(ic)
return 3;
return status;
}
}
No comments:
Post a Comment