**
Game Arena Project -- First Semester Student
**
So our Programming Prof gave us a nice semester project as a fresher. 'Game Arena' in Java.
It's been four months since university started, and boy was it a ride. I was so thrilled when the professor cited how our project would involve a list of console-based games. I've enjoyed games since childhood, and the idea of creating one on my own reminded me why I was a CS student in the first place. My other pre-med background friends panicked at the notion where I stood grinning, annoying them like the prodigy I 'appear' to be. :P
Here's something my dad told me:
"You should always stay a few steps ahead of everyone around you."
Yeah, I enjoy breaking down complex tasks into simpler ones and working on one bit at a time. I wonder what recruiters look for in employees these days.
Anyways, our professor is a highly experienced person in his filed. He had this strategy of having us challenge our problem-solving skills by assigning us a single game each week, out of the complete arena, such that each game tested a new area of programming concepts. The first one was Hangman, a beautiful demonstration of utilising static arrays, filling them through counters, and keeping track of game loop. I really enjoyed almost every game development process, especially this Hangman one.
I remember sitting on my bed, writing DETAILED comments about how I will be executing each part of the game, from the word bank to the letter check and storing guesses. As we learnt newer concepts like I/O and File Handling, our professor instructed us to integrate those methods in our existent programs. It was quite fun seeing those long lines of code in my 'lunar pink' themed VS Code. It is then that coding, becomes art.
Followed by Match the Tiles (absolutely a challenge), Word Scramble and Tic-Tac-Toe, I found out that my love for building evolved with each game I completed. There was an increasing fire of improvement crackling inside me. I wanted to instigate GUI, and give some more life to the games at some point. Regardless, they were still fun somehow in the black-and-white console.
I wouldn't lie, I was pretty much self-confident with the initial games and avoided GPT help to the maximum, however Tic-Tac-Toe really knackered me out. Perhaps I lost the mood up til that point, or got lazier as the finals neared. Still, I did what I could to develop a nice logic for the AI part of this game, i.e. Single Player Mode. That's when it occurred to me that AI pretty much in this case was just man-made code. It still needed human intervention, well at least at a beginning point like this. My original version was just a bunch of redundant 'if-else' statements, and I resented the copy-paste garbage. I prompted GPT to suggest me a better way of handling repetition, and that's when I utilised a nice helper method by a 3D array that held all the grid indices of the 2D Tic Tac Toe grid.
Here's a snippet of that logic!
//This method will help us loop through all the available winning lines of the grid and check if there are two matching entries. Strategy 1: Winning Move ; Strategy 2 : Blocking Move; Strategy 3 : Default (no win / block)
public static boolean tryLine(int[][] win_line, String[][] display_grid, String targetMark, String compMark)
{
int rowOfCell = 0;//0
int columnOfCell = 0, emptyColumn = 0, emptyRow = 0;
int cellsFilled = 0;
//This will allow us to obtain the row and column no of each cell/block of the grid by using the 3D WIN_LINES array
for(int i = 0; i < 3; i++)
{
rowOfCell = win_line[i][0]; //Just access columns of the 2D array (j=0,j=1)
columnOfCell = win_line[i][1];
//Now we will see if the cell at this row and column of the grid is empty or filled with targetMark
if(display_grid[rowOfCell][columnOfCell].equals(targetMark))
{
cellsFilled++;
}
//We need to find an empty cell in the selected line of the display_grid
else if(!display_grid[rowOfCell][columnOfCell].equals("X") && !display_grid[rowOfCell][columnOfCell].equals("O") )
{
emptyColumn = columnOfCell;
emptyRow = rowOfCell;
}
}
//Find if 2 grids have been filled and only 1 is empty
if (cellsFilled == 2 &&
!display_grid[emptyRow][emptyColumn].equals("X") && !display_grid[emptyRow][emptyColumn].equals("O"))
{
display_grid[emptyRow][emptyColumn] = compMark;
return true;
}
return false;
}
public static String[][] computerMark(String[][] display_grid, String compMove, String playerMove)
{
int[][][] win_lines = {
{{0,0},{0,1},{0,2}}, // row 0
{{1,0},{1,1},{1,2}}, // row 1
{{2,0},{2,1},{2,2}}, // row 2
{{0,0},{1,0},{2,0}}, // col 0
{{0,1},{1,1},{2,1}}, // col 1
{{0,2},{1,2},{2,2}}, // col 2
{{0,0},{1,1},{2,2}}, // diagonal
{{0,2},{1,1},{2,0}} // other diagonal
};
for(int i = 0; i < win_lines.length; i++)
{
//STEP 1: Check for a winning move
if(tryLine(win_lines[i], display_grid, compMove, compMove))
{
return display_grid;
}
//STEP 2 : Check for a blocking move
else if(tryLine(win_lines[i], display_grid, playerMove, compMove))
{
return display_grid;
}
}
// STEP 3: Default Move (No win or block: play the first available spot)
boolean isAvailable = false;
for (int k = 0; k < 3; k++)
{
for (int j = 0; j < 3; j++)
{
isAvailable = false;
// Check if the grid is available
do{
int random_i = (int)(Math.random() * 3);
int random_j = (int)(Math.random() * 3);
if (!display_grid[random_i][random_j].equals(compMove) && !display_grid[random_i][random_j].equals(playerMove))
{
display_grid[random_i][random_j] = compMove;
isAvailable = true;
return display_grid;
}
}while(isAvailable == false);
}
}
return display_grid;
}
I'm also linking my GitHub link here if you'd like to see my project. Let me know your experience of semester projects!
Adios! (p.s. I know this post is no formal writing, and well, I guess I like freestyle :P)
Top comments (1)
github.com/mew1allison/Game_Arena_...
Check the Source Code to my Game Arena Project :)