# The Game

### From Progteam

Sorter: | mlc413 |
---|---|

Source: | Unknown |

Link: | http://acm.pku.edu.cn/JudgeOnline/problem?id=1970 |

**The Game** is problem number 1970 on the Peking University ACM
site.

This should be fixed/better now; there was a check missing for diagonal wins...I haven't gotten around to signing up/submitting the code to PKU yet. - Nina

This problem asks you to determine who, if anyone, has won a game of connect five.

Somewhat confusing : " But, a player does not win the game if more than five stones of the same color were put consecutively. "

My code gets the right answer for the test input but is missing some edge case, possibly related to the above statement. Any insight would be awesome.

import java.util.*; public class Main{ public static Scanner in; public static void main(String[] args){ in=new Scanner(System.in); doStuff(); } public static void doStuff(){ int N=in.nextInt(); //System.out.println(N + " cases"); for(int i=0;i<N;i++){ solve(); } } public static void solve(){ boolean found = false; int board[][] = new int[21][21]; for(int i=1; i<= 19; i++){ for(int j=1; j <= 19; j++){ board[i][j] = in.nextInt(); } } for(int i=1; i<=19; i++){ for(int j=1; j<=19; j++){ if(board[i][j] != 0){ boolean n = getNeighbors(i,j,board); if(n){ System.out.println(board[i][j]); System.out.println(i + " " + j); i=100; j=100; found = true; } } } } if(!found){ System.out.println(0); } } public static boolean getNeighbors(int i, int j, int[][] board){ int num = board[i][j]; //row case if( i < 16 && (board[i+1][j] == num ) && ( board[i+2][j] == num ) && ( board[i+3][j] == num ) && ( board[i+4][j] == num ) && board[i+5][j] != num && board[i-1][j] != num ){ return true; } //column case if( j < 16 && board[i][j+1] == num && board[i][j+2] == num && board[i][j+3] == num && board[i][j+4] == num && board[i][j+5] != num && board[i][j-1] != num ) return true; //diagonal - top left to bottom right if( i<16 && j < 16 && board[i+1][j+1] == num && board[i+2][j+2] == num && board[i+3][j+3] == num && board[i+4][j+4] == num && board[i+5][j+5] != num && board[i-1][j-1] != num ) return true; //diagonal - bottom left to top right if( i<16 && j > 4 && board[i+1][j-1] == num && board[i+2][j-2] == num && board[i+3][j-3] == num && board[i+4][j-4] == num && board[i+5][j-5] != num && board[i-1][j+1] != num ) return true; return false; } }