tzure / chess-game Goto Github PK
View Code? Open in Web Editor NEWA chess game written in C, Includes a GUI implementation.
A chess game written in C, Includes a GUI implementation.
In the main function of your Chess.c file, you have 3 game loops inside one main loop.
It is always a good idea to separate these loops to different functions. it will keep your main function short and readable.
i.e. instead of reading a huge function that has many ifs and whiles, you can see a short function like this:
if (game_mode == SETTINGS){
settingsScreen();
} else if (game_mode == TWO_PLAYER) {
gameScreen(TWO_PLAYER);
} else if (game_mode == PLAYER_VS_COMP) {
gameScreen(PLAYER_VS_COMP);
}
once again, since the loops for the pvp and pvc are nearly identical, you can write the code once, and have parameters decide what to do inside the function.
Inside the file minimax.c, on line 23, you have an else.
it is not needed for the flow of the function, and the function will work exactly the same if you delete the else line of the code, and lower the rest of the function by one indentation.
i.e.
if (something) {
return;
} else {
doSomethingElse();
}
is exactly the same as
if (something) {
return;
}
doSomethingElse();
This pattern is called "Early return", and it helps by making your function seem less complex by having less indentations and less if/else branching.
In the minimax.c file, inside the minimax function, you have two very similar for loops, at line 28 and 58.
consider extracting that whole code to another function, so that you don't need to write the same code twice, or, you can turn it into only one loop, and parameterising the differences between them.
i.e. instead of having two for loops, have one that does different things according to parameters.
so instead of
score = minimax(..., ..., ..., 0);
you can use
score = minimax(..., ..., ..., !is_max_turn);
and you can add both the if (score < min_val){
and if (score > max_val){
lines by turning them into one parameter val
, and adding another condition like so:
if (is_max_turn && score > val) {
val = score;
best_move_index = i;
}
if (!is_max_turn && score < val){
val = score;
best_move_index = i;
}
This will surely make the entire file shorter, and easier to understand.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.