Skip to content

Instantly share code, notes, and snippets.

@Capital-EX
Created November 20, 2023 16:35
Show Gist options
  • Save Capital-EX/97b651c32911b6d6387e46a80dcce75b to your computer and use it in GitHub Desktop.
Save Capital-EX/97b651c32911b6d6387e46a80dcce75b to your computer and use it in GitHub Desktop.
#include <ObjFW/OFArray.h>
#include <ObjFW/OFData.h>
#include <ObjFW/OFMutableArray.h>
#import <ObjFW/OFObject.h>
#include <stddef.h>
#include <stdio.h>
#include <ObjFW/OFString.h>
#include <ObjFW/OFStream.h>
#import <ObjFW/OFStdIOStream.h>
#import <ObjFW/ObjFW.h>
typedef enum {
HIT,
HAS,
MISS,
EMPTY,
} GuessResult;
size_t find_char(OFString *s, OFUnichar c, bool matched[5]) {
for (size_t i = 0; i < [s length]; i++) {
if (c == [s characterAtIndex: i] && !matched[i]) { matched[i] = true; return i; }
}
return OFNotFound;
}
void print_state(OFString *previous_guesses[6], GuessResult results[6][5]) {
int row = 0;
for (; previous_guesses[row] != nil && row < 6; row++) {
for (int column = 0; column < 5; column++) {
OFUnichar ch = [previous_guesses[row] characterAtIndex: column];
switch (results[row][column]) {
case HIT:
[OFStdOut writeFormat: @"\33[92m%C\33[0m", ch]; break;
case HAS:
[OFStdOut writeFormat: @"\33[93m%C\33[0m", ch]; break;
case MISS:
[OFStdOut writeFormat: @"\33[91m%C\33[0m", ch]; break;
default: continue;
}
}
printf("\n");
}
for (int i = 0; i < (6 - row); i++) {
for (int i = 0; i < 5; i++) { [OFStdOut writeString: @"_"]; }
printf("\n");
}
}
@interface HelloWorldApp: OFObject <OFApplicationDelegate>
@end
OF_APPLICATION_DELEGATE(HelloWorldApp)
@implementation HelloWorldApp
- (void)applicationDidFinishLaunching: (OFNotification *)notification
{
int tries = 6;
bool won = false;
GuessResult results[6][5] = {
{ EMPTY, EMPTY, EMPTY, EMPTY, EMPTY },
{ EMPTY, EMPTY, EMPTY, EMPTY, EMPTY },
{ EMPTY, EMPTY, EMPTY, EMPTY, EMPTY },
{ EMPTY, EMPTY, EMPTY, EMPTY, EMPTY },
{ EMPTY, EMPTY, EMPTY, EMPTY, EMPTY },
{ EMPTY, EMPTY, EMPTY, EMPTY, EMPTY },
};
OFString *string = [OFString stringWithContentsOfFile:@"test.txt"];
OFArray<OFString *> *words = [string componentsSeparatedByString: @"\n"];
// TODO: push fix for OFMutableArray
OFString *previous_guesses[6] = {nil, nil, nil, nil, nil, nil};
OFString *guess = nil;
OFString *target_word = @"sonar";
// [words objectAtIndex:OFRandom64() % [words count]];
while (tries) {
print_state(previous_guesses, results);
while (true) {
guess = [OFStdIn readLine];
if ([guess length] != 5) { [OFStdOut writeLine: @"Please enter 5 letters."]; continue; }
if (![words containsObject: guess]) { [OFStdOut writeLine: @"Word is not recognized."]; continue; }
break;
}
previous_guesses[6-tries] = guess;
int guess_index = 0, matches = 0;
bool matched[5] = { false, false, false, false, false };
const OFUnichar *guessed_letters = [guess characters];
for (size_t i = 0; i < [guess length]; i++) {
size_t index = find_char(target_word, guessed_letters[i], matched);
if (index == OFNotFound) { results[6 - tries][i] = MISS; }
else if (index != i) { results[6 - tries][i] = HAS; }
else { results[6 - tries][i] = HIT; matches += 1; }
}
if (matches == 5) { won = true; break; } tries -= 1; printf("\n\n");
}
print_state(previous_guesses, results);
if (won) { [OFStdOut writeLine: @"You Won!"]; }
else { [OFStdOut writeLine:
[OFString stringWithFormat: @"You lost...\nWord was: %S", [target_word characters]]]; }
[OFApplication terminate];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment