1

I'm making a checkers game (for recreational purposes). And I'm using C. I was thinking of drawing the board with:

  • ASCII 219 (full square) █
  • ASCII (space) ' '

But I'd like to maintain the full background with letters (negative like).

I've searched in ways to color the characters like here, but it doesn't work in Windows (at least not for me...) and I'd like a simple solution.

Does anybody have an idea? Is there a "thing" like "negative O"?


Temporary workaround:

To maintain it universal, I'll keep it all ASCII with ASCII art. And use only the "white" squares. Since it's just checkers, I'll only use one type of square. For Chess it would be different... If a better solution should apear, I'll change the answer.

Rui Silva
  • 413
  • 1
  • 5
  • 12
  • Depends on the terminal you are using. – TDk Nov 03 '17 at 12:20
  • 1
    There's no such thing as ASCII 219. – r3mainer Nov 03 '17 at 12:20
  • @squeamishossifrage actually [there is](http://www.theasciicode.com.ar/extended-ascii-code/block-graphic-character-ascii-code-219.html) – TDk Nov 03 '17 at 12:20
  • @squeamishossifrage, I've updated the question so you can see the character. And I'm refering to the character with ascii value of 219. – Rui Silva Nov 03 '17 at 12:21
  • @TheophileDano, I'm searching for a solution that doesn't depend on the terminal. If possible.... ;) – Rui Silva Nov 03 '17 at 12:23
  • 3
    @TheophileDano That's not ASCII, it's code page 437. Even the page you linked says so. – interjay Nov 03 '17 at 12:23
  • @RuiSilva well, if you want to print the characters of your board, you need that to happen in a terminal window... otherwise you are dealing with GUI framework (openGL probably). – TDk Nov 03 '17 at 12:26
  • 2
    If you want to be portable across terminal types/emulations, it would be advisable to use an abstraction layer like libncurses or the like (see for example `attron()` with `A_REVERSE`). However, it is not guaranteed, that _all_ terminals will support inverted characters. – Ctx Nov 03 '17 at 12:26
  • @Ctx, My goal is to avoid problems, and my idea is to use ascii, But if it's not possible, I'M ALL EARS!!! :) – Rui Silva Nov 03 '17 at 12:27
  • It would be easier to use different backgrounds (and again, space character) instead of special codepoint. It is supported via ANSI escape sequences on many modern tty emulators for Unix and [`SetConsoleTextAttribute`](https://learn.microsoft.com/en-us/windows/console/setconsoletextattribute) in Wandows. – myaut Nov 03 '17 at 12:29
  • Use ANSI escape codes: https://stackoverflow.com/questions/3219393/stdlib-and-colored-output-in-c – Andrejs Cainikovs Nov 03 '17 at 12:54
  • @AndrejsCainikovs, That isn't working for me. I've tried that before. – Rui Silva Nov 03 '17 at 12:58
  • In this case you obviously did something wrong, unless your terminal is from 70's. Here is the table with a white character on black background, which is what you want: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors – Andrejs Cainikovs Nov 03 '17 at 13:00
  • 1
    I'm disappointed nobody has mentioned `ESC [ 7 m` yet. It's the escape code for reverse, which will work on non-color-supporting terminals, and also will do the right thing when the terminal's default colors aren't what you expect. –  Nov 03 '17 at 13:38

3 Answers3

2

I'd use ANSI escape codes. All modern terminals supports them.

Example:

#include <stdio.h>
#include <string.h>

#define ANSI_COLOR1 "\33[38;5;0;48;5;255m"
#define ANSI_COLOR2 "\33[38;5;255;48;5;0m"
#define ANSI_RESET  "\33[m"

int main(int argc, char const *argv[])
{
    printf(ANSI_COLOR1 "XXX" ANSI_RESET);
    printf(ANSI_COLOR2 "XXX" ANSI_RESET "\n");

    return 0;
}

Output:

enter image description here

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
0

Escape codes will not work on MS-DOS terminals, so you might want to use a cross-platform coloring library, like this. However since you are making a game, and are willing to have an image library dependency, you might want to go with something like: https://github.com/rossy/img2xterm

mnistic
  • 10,866
  • 2
  • 19
  • 33
  • Mentioned library will does not have support for MS-DOS colors either. Furthermore, if you compile it for non-Windows target, [it will use ANSI escape codes](https://github.com/VittGam/colorprintf/blob/master/colorprintf.h#L99). – Andrejs Cainikovs Nov 03 '17 at 13:10
  • Well that's point, use `SetConsoleTextAttribute` on Windows, escape codes when not on Windows. – mnistic Nov 03 '17 at 13:41
  • MS-Dos is not Windows! – Andrejs Cainikovs Nov 03 '17 at 13:42
  • But it's an MS-DOS emulation terminal, no? – mnistic Nov 03 '17 at 13:43
  • [I had a closer look at this topic](https://en.wikipedia.org/wiki/ANSI_escape_code#Windows_and_DOS). You are right: Windows console lacked ANSI code support up to Windows 10, so for Windows console prior to Windows 10 you should use the function you mentioned. – Andrejs Cainikovs Nov 03 '17 at 13:53
  • Huh, looks like they quietly started adding support for escape codes with TH2 last year, I wasn't even aware of it. Well, this method should work either way. – mnistic Nov 03 '17 at 14:00
0

As others have suggested, I'd probably try to get ANSI colour codes working. But if this turns out to be difficult, you could always use ASCII art as a backup plan.

Something like this perhaps:

+-------+-------+-------+-------+-------+-------+-------+-------+
|-------|       |-------|       |-------|       |-------|       |
|-------|       |-------|       |-------|       |-------|       |
|-------|       |-------|       |-------|       |-------|       |
+-------+-------+-------+-------+-------+-------+-------+-------+
|  o8o  |--o8o--|  o8o  |--o8o--|  .:.  |--.:.--|  .:.  |--.:.--|
| 88888 |-88888-| 8 K 8 |-8 K 8-| ::::: |-:::::-| : K : |-: K :-|
|  *8*  |--*8*--|  *8*  |--*8*--|  ':'  |--':'--|  ':'  |--':'--|
+-------+-------+-------+-------+-------+-------+-------+-------+
r3mainer
  • 23,981
  • 3
  • 51
  • 88