Giter Site home page Giter Site logo

chess-game's Issues

split every game loop / screen loop to a separate function in Chess.c:main

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.

Use early return at minimax.c:23

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.

Consider extracting a function for calculating minimum/maximum score in minimax.c

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.