Skip to content

Commit 842355e

Browse files
Final adjustments
1 parent 05444ce commit 842355e

File tree

4 files changed

+82
-21
lines changed

4 files changed

+82
-21
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
CROSSCXX := x86_64-w64-mingw32-g++
22

33
main:
4-
$(CROSSCXX) src/main.cpp src/ui.cpp src/wordparser.cpp -Wall -Wno-sign-compare -O2 -o main.exe
4+
$(CROSSCXX) src/main.cpp src/ui.cpp src/wordparser.cpp -Wall -Wno-sign-compare -O2 -static -o main.exe

include/ui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void term_moveCursor(short x, short y); // Move cursor to x and y position speci
1414
uint8_t term_createColorPair(color_t foreground, color_t background); // Create color pair with foreground (text) and background colors specified
1515
void term_enableColorPair(uint8_t colorPairIndex); // Activate color pair with id specified
1616
void term_resetColorPair(); // Set color pair to white text on black background
17+
void term_setCursorVisibility(bool visible); // Change cursor visibility (0 or 1)
1718
void term_getTermSize(short& x, short& y); // Get terminal size
1819

1920
#endif

src/main.cpp

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ void redrawLetters(short term_maxx, short term_maxy, char playerLetters[7], uint
327327
term_resetColorPair();
328328
}
329329

330-
void fillPlayerLetters(vector<char> letters, char playerLetters[7])
330+
void fillPlayerLetters(vector<char>& letters, char playerLetters[7])
331331
{
332332
static mt19937 mt(time(nullptr));
333333
for (int i = 0, id; i<7; ++i)
@@ -346,14 +346,17 @@ int emptyLettersOnHand(char letters[7])
346346
{
347347
int ret = 0;
348348
for (int i = 0; i<7; ++i)
349-
ret += letters[i]==0;
349+
ret += (letters[i]==0);
350350

351351
return ret;
352352
}
353353

354354
int game()
355355
{
356356
term_clear();
357+
358+
term_setCursorVisibility(1);
359+
357360
uint8_t input, x = 7, y = 7, direction = 0;
358361
// direction 0 - undecided, 1-x, 2-y
359362

@@ -686,7 +689,7 @@ int game()
686689
if ((letters.size()+emptyLettersOnHand(playerLetters[turn]))==0)
687690
{
688691
// if all letters are used
689-
return playerScore[0]<playerScore[1];
692+
return (playerScore[0]!=playerScore[1]?(playerScore[0]<playerScore[1])+1:0);
690693
}
691694

692695
turn = !turn;
@@ -730,8 +733,16 @@ int game()
730733

731734
fillPlayerLetters(letters, playerLetters[turn]);
732735

736+
term_moveCursor(20, 3);
737+
cout << (letters.size()+emptyLettersOnHand(playerLetters[turn]));
738+
if ((letters.size()+emptyLettersOnHand(playerLetters[turn]))==0)
739+
{
740+
// if all letters are used
741+
return (playerScore[0]!=playerScore[1]?(playerScore[0]<playerScore[1])+1:0);
742+
}
743+
733744
term_moveCursor(COORDSX(-2), COORDSY(-5));
734-
cout << " ";
745+
cout << " ";
735746

736747
turn = !turn;
737748

@@ -747,11 +758,12 @@ int game()
747758
}
748759
term_moveCursor(COORDSX(x), COORDSY(y));
749760
}
750-
return playerScore[0]<playerScore[1];
761+
return (playerScore[0]!=playerScore[1]?(playerScore[0]<playerScore[1])+1:0);
751762
}
752763

753764
int main() // menu
754765
{
766+
term_setCursorVisibility(0);
755767
init:
756768
term_init();
757769

@@ -813,35 +825,49 @@ int main() // menu
813825
case '1':
814826
{
815827
result = game();
828+
term_setCursorVisibility(0);
816829
break;
817830
}
818831
case '3':
819832
{
820833
term_clear();
834+
Sleep(100);
821835
term_moveCursor(term_maxx/2-4, term_maxy/2-10);
822836
cout << "Scrabble";
837+
Sleep(100);
823838
term_moveCursor(term_maxx/2-20, term_maxy/2-8);
824839
cout << "The goal of the game is to get points";
840+
Sleep(100);
825841
term_moveCursor(term_maxx/2-20, term_maxy/2-7);
826842
cout << "by using the letters you have. You win";
843+
Sleep(100);
827844
term_moveCursor(term_maxx/2-20, term_maxy/2-6);
828845
cout << "when there are no more letters left.";
846+
Sleep(100);
829847
term_moveCursor(term_maxx/2-20, term_maxy/2-5);
830848
cout << "You can move only to tiles with letters";
849+
Sleep(100);
831850
term_moveCursor(term_maxx/2-20, term_maxy/2-4);
832851
cout << "in or next to them. You can see your";
852+
Sleep(100);
833853
term_moveCursor(term_maxx/2-20, term_maxy/2-3);
834854
cout << "hand below the playfield. During your";
855+
Sleep(100);
835856
term_moveCursor(term_maxx/2-20, term_maxy/2-2);
836857
cout << "turn, you can do either:";
858+
Sleep(100);
837859
term_moveCursor(term_maxx/2-20, term_maxy/2-1);
838860
cout << "1. Place one or more letters to form";
861+
Sleep(100);
839862
term_moveCursor(term_maxx/2-20, term_maxy/2);
840863
cout << "one or more words";
864+
Sleep(100);
841865
term_moveCursor(term_maxx/2-20, term_maxy/2+1);
842866
cout << "2. Pass the turn and get zero points";
867+
Sleep(100);
843868
term_moveCursor(term_maxx/2-20, term_maxy/2+2);
844869
cout << "3. Exchange some letters for new ones";
870+
Sleep(100);
845871
term_moveCursor(term_maxx/2-20, term_maxy/2+3);
846872
cout << "Good luck and have fun!";
847873
term_getch();
@@ -852,16 +878,22 @@ int main() // menu
852878
term_clear();
853879
term_moveCursor(term_maxx/2-15, term_maxy/2-3);
854880
cout << "Arrow keys - move on the field";
881+
Sleep(100);
855882
term_moveCursor(term_maxx/2-15, term_maxy/2-2);
856883
cout << "Letter - choose / place a letter";
884+
Sleep(100);
857885
term_moveCursor(term_maxx/2-15, term_maxy/2-1);
858886
cout << "Enter - calculate score";
887+
Sleep(100);
859888
term_moveCursor(term_maxx/2-15, term_maxy/2);
860889
cout << "Space - pass your turn";
890+
Sleep(100);
861891
term_moveCursor(term_maxx/2-15, term_maxy/2+1);
862892
cout << "Ctrl-R - exchange letters";
893+
Sleep(100);
863894
term_moveCursor(term_maxx/2-15, term_maxy/2+2);
864895
cout << "Backspace - return a letter";
896+
Sleep(100);
865897
term_moveCursor(term_maxx/2-15, term_maxy/2+3);
866898
cout << "Ctrl-C - finish the game";
867899
term_getch();
@@ -870,31 +902,34 @@ int main() // menu
870902
case '5':
871903
{
872904
term_clear();
873-
term_moveCursor(term_maxx/2-16, term_maxy/2-2);
874-
cout << "Scrum Leader - SAKostin";
905+
term_moveCursor(term_maxx/2-20, term_maxy/2-2);
906+
cout << "Scrum Leader - SAKostin";
875907
Sleep(200);
876-
term_moveCursor(term_maxx/2-16, term_maxy/2-1);
908+
term_moveCursor(term_maxx/2-20, term_maxy/2-1);
877909
cout << "Backend Developer - SVRomanchenko";
878910
Sleep(200);
879911

880-
term_moveCursor(term_maxx/2-16, term_maxy/2+1);
912+
term_moveCursor(term_maxx/2-20, term_maxy/2+1);
881913
cout << "Frontend Developer - OVBondarenko";
882914
Sleep(200);
883-
term_moveCursor(term_maxx/2-16, term_maxy/2+2);
884-
cout << "Designer - SAChapkina";
915+
term_moveCursor(term_maxx/2-20, term_maxy/2+2);
916+
cout << "Designer - SAChapkina";
885917
term_getch();
886918
goto init;
887919
}
888920
default:
889921
{
890922
term_deinit();
923+
term_setCursorVisibility(1);
891924
return 0;
892925
}
893926
}
894927

895928
term_deinit();
929+
term_setCursorVisibility(1);
896930

897-
cout << "Player " << result+1 << " won!\n";
931+
if (result) cout << "Player " << result << " won!\n";
932+
else cout << "Tie!\n";
898933
term_getch();
899934
return 0;
900935
}

src/ui.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ void term_init()
2121

2222
term_clear();
2323

24+
// set cursor to box
25+
26+
CONSOLE_CURSOR_INFO cursorInfo;
27+
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
28+
cursorInfo.dwSize = 100;
29+
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
30+
2431
// Initialise all color pairs to white text on black background (default)
2532

2633
for (uint8_t i = 0; i<16; ++i)
@@ -33,6 +40,7 @@ void term_init()
3340
void term_deinit()
3441
{
3542
term_resetColorPair();
43+
term_clear();
3644
term_moveCursor(0,0);
3745
}
3846

@@ -111,17 +119,34 @@ void term_getTermSize(short& x, short& y)
111119
{
112120
CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
113121
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &bufferInfo);
114-
x = bufferInfo.srWindow.Right - bufferInfo.srWindow.Left;
115-
y = bufferInfo.srWindow.Bottom - bufferInfo.srWindow.Top;
122+
x = bufferInfo.srWindow.Right - bufferInfo.srWindow.Left+1;
123+
y = bufferInfo.srWindow.Bottom - bufferInfo.srWindow.Top+1;
124+
}
125+
126+
void term_setCursorVisibility(bool visible)
127+
{
128+
CONSOLE_CURSOR_INFO cursorInfo;
129+
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
130+
cursorInfo.bVisible = visible;
131+
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
116132
}
117133

118134
void term_clear()
119135
{
120-
short x, y;
121-
DWORD counter;
122-
term_getTermSize(x, y);
123-
--x; // no stack overflow
124-
--y;
125-
FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), ' ', ((int)x)*y, {0,0}, &counter);
136+
short term_maxx, term_maxy;
137+
term_getTermSize(term_maxx, term_maxy);
138+
term_resetColorPair();
139+
term_moveCursor(0,0);
140+
141+
string fullLine = "";
142+
143+
for (int x = 0; x<term_maxx; ++x)
144+
fullLine += ' ';
145+
146+
for (int y = 0; y<term_maxy; ++y)
147+
{
148+
term_moveCursor(0, y);
149+
cout << fullLine;
150+
}
126151
term_moveCursor(0,0);
127152
}

0 commit comments

Comments
 (0)