Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Post top tier code

Name: Anonymous 2014-07-22 13:29

Can be yours or not, I just want to see something good.

Name: Anonymous 2014-08-03 12:13

Name: Anonymous 2014-08-03 13:03

>>41
Actually, it will unable to build the PCI board that has a function which satisfied the PCI standard with only standard logic ICs. […] Therefore, it is possible that a PCI board to operate only as a target device uses small-scale PLD and it is made.
Smells like Markov chains.

Name: Anonymous 2014-08-03 16:14

>>40
War is peace, freedom is slavery, static typing is a failure.
Also, all languages are statically typed, even Scheme.

Name: Anonymous 2014-08-03 16:55

>>38
The typical webmonkey toolset?

Name: Anonymous 2014-08-03 17:39

>>38
Why are people so ignorant of their history?

Name: Anonymous 2014-08-03 18:20

So basically like prolog then.

Name: Anonymous 2014-08-06 18:41

take Int x ... from Range(100) as numbers.
loop for n in numbers do
take Bool from NModM(n; 3) as fizz.
take Bool from NModM(n; 5) as buzz.
if not fizz and not buzz then
PrintInt(n).
else
if fizz then Print("Fizz"). fi.
if buzz then Print("Buzz"). fi.
fi.
Print("\n").
done.

proc Range take Int as len.
create Int x len as range.
declare Int of 1 as i.
loop while i <= len do
range:i <- i.
i <- i + 1.
done.
corp.

Name: Anonymous 2014-08-07 11:07

>>47
Imperative loops are in no way "top-tier", dumbass.

Name: Anonymous 2014-08-07 11:12

>>47
What language is this? Seems like a weird version of Ada

Name: Anonymous 2014-08-07 17:43

>>48,49
It's an experiment I'm working on. Part of the design concept is being able to run in constant stack space (ie. static allocation of procedure activation records) so there are some limitations on calling functions. The compiler doesn't actually work yet though.

Name: Anonymous 2014-08-07 17:47

>>50
Part of the design concept is being able to run in constant stack space (ie. static allocation of procedure activation records)
I remember some Ada users did something like this, I am not so sure

Name: Anonymous 2014-08-08 6:21

>>50
Why do this when a static call graph analysis will tell you the maximum stack extent anyway (provided recursion is not allowed)?

Name: Anonymous 2014-08-08 6:22

>>52
recursion is not allowed
recursion is not allowed
recursion is not allowed

Imperative numbskulls have gone totally cuckoo.

Name: Anonymous 2014-08-08 6:43

Nah, you can still do recursion in imperative stylee

Name: Anonymous 2014-08-08 9:40

>>53
It's funny because in OpenCL recursion is not allowed, not even tail recursion

Name: Anonymous 2014-08-08 9:51

>>55
Well, I didn't have any doubts that OpenCL is a failure. Also, nice dubs.

Name: >>47,50 2014-08-08 23:13

>>55
Tail calls are in fact the only place I allow recursion, and at the moment, only to the calling function. I'm sure I could loosen up those rules with better analysis, but it's not a priority right now.

Name: Anonymous 2014-08-09 0:38

If recursion is just a feedback loop (current state x -> next state y via function z)... isn't all `imperative' coding recursive?

Name: Anonymous 2014-08-09 0:51

loop
y = z(x)
x = y

is the same as y = z(z(z(z(z(z(x))))))

except you have access to other variables in the former?

Name: Anonymous 2014-08-09 1:15

functions are good for cleaning up junk variables and redundant code though =)

Name: Anonymous 2014-08-09 1:42

Consider this

printf( fibs( fibs( fibs( 1:n ))))

Name: Anonymous 2014-08-09 1:51

>>58
coding
Shalom!

Name: Anonymous 2014-08-09 2:57

ah, just about needs a bignum library... fibs(fibs(10)) is nearly too big for 64 bit ints =)

maybe fibx(n) = fibs(n+8) - fibs(n+4) + fibs(n+2)

Name: Anonymous 2014-08-09 3:35

fibs(2n) = fibs(n)^2 + fibs(n+1)^2 ?

Name: Anonymous 2014-08-09 3:50

>>63,64 hasn't read SICP.

Name: Anonymous 2014-08-09 4:04

Fuerst Hash.

Faster than xxhash, MumurHash, SpookyHash, SBox, CityHash, and all FNV variants. Even faster than Intel's CRC32 instructions. It also has better properties and less collisions than all of the above, due to the 256-bit accumulation window. None of the plebs know about it, and probably never will. They prefer to use shit.

I hand rolled this for x86. x86-64 version was easier and cleaner, due to the larger register count and native 64-bit x 64-bit -> 128-bit integer multiplies.

#
# uint64_t memhash(void const* key, size_t n, uint64_t seed);
#
# Based off of the fast byteswap hash function by Steven Fuerst.
# http://locklessinc.com/articles/fast_hash/
#

.text
.align 16, 0x90

factor128:
.octa 0xd6c573e9c613993d5a379ab38dc5a46b

mix1:
.octa 0x1591aefa5e7e5a171591aefa5e7e5a17

mix2:
.octa 0x2bb6863566c4e7612bb6863566c4e761

.global memhash
.type memhash, @function

memhash:
movl 4(%esp), %ecx # key
movl 8(%esp), %edx # n
movq 12(%esp), %xmm2 # seed
pushl %edi
movd %edx, %xmm1
pushl %esi
movdqa (factor128), %xmm3
movdqa (mix1), %xmm6
movdqa (mix2), %xmm7
cmpl $32, %edx
jb 2f
mov %edx, %eax
shr $5, %eax
testb $15, %cl
jne 5f

# Aligned main loop, process 32-byte aligned chunks at a time

.align 8, 0x90
1: paddq (%ecx), %xmm1
paddq 16(%ecx), %xmm2
pmullw %xmm3, %xmm1
pmullw %xmm3, %xmm2
add $32, %ecx
movdqa %xmm1, %xmm0
punpckhbw %xmm2, %xmm1
punpcklbw %xmm0, %xmm2
dec %eax
jne 1b
pxor %xmm2, %xmm1

# Check for remaining chunk, otherwise extract 128 bits state and proceed to final mix

2: testb $31, %dl
jne 3f
movhlps %xmm1, %xmm2
jmp 4f

# Hash remaining chunk, up to 31-bytes in size

3: testb $16, %dl
je 3f
movdqu (%ecx), %xmm0
add $16, %ecx
pxor %xmm0, %xmm1
pmullw %xmm3, %xmm1
3: pxor %xmm3, %xmm3
movhlps %xmm1, %xmm2
testb $8, %dl
je 3f
movq (%ecx), %xmm0
add $8, %ecx
pxor %xmm0, %xmm1
3: testb $4, %dl
je 3f
movd (%ecx), %xmm3
add $4, %ecx
3: testb $2, %dl
je 3f
movzwl (%ecx), %eax
psllq $16, %xmm3
movd %eax, %xmm0
add $2, %ecx
paddq %xmm0, %xmm3
3: testb $1, %dl
je 3f
movzbl (%ecx), %eax
psllq $8, %xmm3
movd %eax, %xmm0
paddq %xmm0, %xmm3
3: pxor %xmm3, %xmm2

# Use 3x 128bit multiply and xorshift for final mix

4: # Iteration #0

pshufd $0b10110001, %xmm6, %xmm3
punpckldq %xmm1, %xmm1
pmuludq %xmm1, %xmm3 # [H|G|F|E] = [hash1.high * mix1.high | hash1.low * mix1.high]
punpckldq %xmm2, %xmm2
pmuludq %xmm6, %xmm1 # [D|C|B|A] = [hash1.high * mix1.low | hash1.low * mix1.low]
movd %xmm3, %ecx
pshufd $0b11100001, %xmm1, %xmm1
pshufd $0b11100001, %xmm3, %xmm3
movd %xmm1, %edx
movd %xmm3, %esi
pshufd $0b11010010, %xmm1, %xmm1
pshufd $0b01110010, %xmm3, %xmm3
movd %xmm1, %eax
xor %edi, %edi
pshufd $0b00100111, %xmm1, %xmm1
add %eax, %edx # X = B + C
movd %xmm1, %eax
adc $0, %eax # Y = D + carry
adc $0, %edi # Z = carry
add %ecx, %edx # X = X + E
punpckhdq %xmm1, %xmm1 # [0|0|0|A]
movd %edx, %xmm0 # [0|0|0|X]
adc %esi, %eax # Y = Y + F + carry
movd %xmm3, %edx
punpckhdq %xmm3, %xmm3
adc $0, %edi # Z = Z + carry
movd %xmm3, %esi
add %edx, %eax # Y = Y + G
pshufd $0b10101111, %xmm7, %xmm3
adc %esi, %edi # Z = Z + H + carry
movd %eax, %xmm5 # [0|0|0|Y]
movd %edi, %xmm4 # [0|0|0|Z]
punpckldq %xmm0, %xmm1 # [0|0|X|A] = [0 | lower 64-bits of hash1 * mix1]
punpckldq %xmm4, %xmm5 # [0|0|Z|Y] = [0 | upper 64-bits of hash1 * mix1]
pmuludq %xmm2, %xmm3 # [h|g|f|e] = [hash2.high * mix2.low | hash2.low * mix2.high]
pxor %xmm0, %xmm0
pmuludq %xmm7, %xmm2 # [d|c|b|a] = [hash2.high * mix2.high | hash2.low * mix2.low]
punpckldq %xmm3, %xmm0 # [e|0|e|0]
pxor %xmm4, %xmm4
paddd %xmm0, %xmm2 # [d+e|c|b+e|a]
punpckhdq %xmm3, %xmm4 # [g|0|g|0]
paddd %xmm4, %xmm2 # [d+e+g|c|b+e+g|a] = [garbage | lower 64-bits of hash2 * mix2]
paddq %xmm5, %xmm2 # hash2 = (hash2 * mix2) + upper_64bits(hash1 * mix1)
pxor %xmm2, %xmm1 # hash1 = hash2 ^ lower_64bits(hash1 * mix1)

# Iteration #1

pshufd $0b10110001, %xmm6, %xmm3
punpckldq %xmm1, %xmm1
pmuludq %xmm1, %xmm3 # [H|G|F|E] = [hash1.high * mix1.high | hash1.low * mix1.high]
punpckldq %xmm2, %xmm2
pmuludq %xmm6, %xmm1 # [D|C|B|A] = [hash1.high * mix1.low | hash1.low * mix1.low]
movd %xmm3, %ecx
pshufd $0b11100001, %xmm1, %xmm1
pshufd $0b11100001, %xmm3, %xmm3
movd %xmm1, %edx
movd %xmm3, %esi
pshufd $0b11010010, %xmm1, %xmm1
pshufd $0b01110010, %xmm3, %xmm3
movd %xmm1, %eax
xor %edi, %edi
pshufd $0b00100111, %xmm1, %xmm1
add %eax, %edx # X = B + C
movd %xmm1, %eax
adc $0, %eax # Y = D + carry
adc $0, %edi # Z = carry
add %ecx, %edx # X = X + E
punpckhdq %xmm1, %xmm1 # [0|0|0|A]
movd %edx, %xmm0 # [0|0|0|X]
adc %esi, %eax # Y = Y + F + carry
movd %xmm3, %edx
punpckhdq %xmm3, %xmm3
adc $0, %edi # Z = Z + carry
movd %xmm3, %esi
add %edx, %eax # Y = Y + G
pshufd $0b10101111, %xmm7, %xmm3
adc %esi, %edi # Z = Z + H + carry
movd %eax, %xmm5 # [0|0|0|Y]
movd %edi, %xmm4 # [0|0|0|Z]
punpckldq %xmm0, %xmm1 # [0|0|X|A] = [0 | lower 64-bits of hash1 * mix1]
punpckldq %xmm4, %xmm5 # [0|0|Z|Y] = [0 | upper 64-bits of hash1 * mix1]
pmuludq %xmm2, %xmm3 # [h|g|f|e] = [hash2.high * mix2.low | hash2.low * mix2.high]
pxor %xmm0, %xmm0
pmuludq %xmm7, %xmm2 # [d|c|b|a] = [hash2.high * mix2.high | hash2.low * mix2.low]
punpckldq %xmm3, %xmm0 # [e|0|e|0]
pxor %xmm4, %xmm4
paddd %xmm0, %xmm2 # [d+e|c|b+e|a]
punpckhdq %xmm3, %xmm4 # [g|0|g|0]
paddd %xmm4, %xmm2 # [d+e+g|c|b+e+g|a] = [garbage | lower 64-bits of hash2 * mix2]
paddq %xmm5, %xmm2 # hash2 = (hash2 * mix2) + upper_64bits(hash1 * mix1)
pxor %xmm2, %xmm1 # hash1 = hash2 ^ lower_64bits(hash1 * mix1)

# Iteration #2

pshufd $0b10110001, %xmm6, %xmm3
punpckldq %xmm1, %xmm1
pmuludq %xmm1, %xmm3 # [H|G|F|E] = [hash1.high * mix1.high | hash1.low * mix1.high]
punpckldq %xmm2, %xmm2
pmuludq %xmm6, %xmm1 # [D|C|B|A] = [hash1.high * mix1.low | hash1.low * mix1.low]
movd %xmm3, %ecx
pshufd $0b11100001, %xmm1, %xmm1
pshufd $0b11100001, %xmm3, %xmm3
movd %xmm1, %edx
movd %xmm3, %esi
pshufd $0b11010010, %xmm1, %xmm1
pshufd $0b01110010, %xmm3, %xmm3
movd %xmm1, %eax
xor %edi, %edi
pshufd $0b00100111, %xmm1, %xmm1
add %eax, %edx # X = B + C
movd %xmm1, %eax
adc $0, %eax # Y = D + carry
adc $0, %edi # Z = carry
add %ecx, %edx # X = X + E
punpckhdq %xmm1, %xmm1 # [0|0|0|A]
movd %edx, %xmm0 # [0|0|0|X]
adc %esi, %eax # Y = Y + F + carry
movd %xmm3, %edx
punpckhdq %xmm3, %xmm3
adc $0, %edi # Z = Z + carry
movd %xmm3, %esi
add %edx, %eax # Y = Y + G
pshufd $0b10101111, %xmm7, %xmm3
adc %esi, %edi # Z = Z + H + carry
movd %eax, %xmm5 # [0|0|0|Y]
movd %edi, %xmm4 # [0|0|0|Z]
punpckldq %xmm0, %xmm1 # [0|0|X|A] = [0 | lower 64-bits of hash1 * mix1]
punpckldq %xmm4, %xmm5 # [0|0|Z|Y] = [0 | upper 64-bits of hash1 * mix1]
pmuludq %xmm2, %xmm3 # [h|g|f|e] = [hash2.high * mix2.low | hash2.low * mix2.high]
pxor %xmm0, %xmm0
pmuludq %xmm7, %xmm2 # [d|c|b|a] = [hash2.high * mix2.high | hash2.low * mix2.low]
punpckldq %xmm3, %xmm0 # [e|0|e|0]
pxor %xmm4, %xmm4
paddd %xmm0, %xmm2 # [d+e|c|b+e|a]
punpckhdq %xmm3, %xmm4 # [g|0|g|0]
paddd %xmm4, %xmm2 # [d+e+g|c|b+e+g|a] = [garbage | lower 64-bits of hash2 * mix2]
paddq %xmm5, %xmm2 # hash2 = (hash2 * mix2) + upper_64bits(hash1 * mix1)
pxor %xmm2, %xmm1 # hash1 = hash2 ^ lower_64bits(hash1 * mix1)

# Place 64-bit result in %edx:%eax and return

popl %esi
movd %xmm1, %eax
pshufd $0b11100001, %xmm1, %xmm1
popl %edi
movd %xmm1, %edx
ret

# Unaligned main-loop, process 32-byte unaligned chunks at a time

.align 16, 0x90
5: movdqu (%ecx), %xmm0
add $32, %ecx
paddq %xmm0, %xmm1
movdqu -16(%ecx), %xmm0
pmullw %xmm3, %xmm1
paddq %xmm0, %xmm2
pmullw %xmm3, %xmm2
movdqa %xmm1, %xmm0
punpckhbw %xmm2, %xmm1
punpcklbw %xmm0, %xmm2
dec %eax
jne 5b
pxor %xmm2, %xmm1
jmp 2b

.size memhash, .-memhash


Also, check em.

Name: Anonymous 2014-08-09 4:05

id0/v0, 1/1, 2/1, 3/2, 4/3, 5/5, 6/8, 7/13, 8/21, 9/34
2*2 + 3*3 = 13
3+4 = 7
2n+1

Name: Anonymous 2014-08-09 4:13

fibs(2n) = fibs(2n+1) - fibs(2(n-1)+1)

The Ultimate Fibs!!! =D

Name: Anonymous 2014-08-09 4:31

/* 2 bit adder in C
if any of the input parameters is bigger than 2 bits, the behavior is undefined
if the result of addition is more than 2 bits, the behavior is undefined.
int add(int a, int b) {
return a + b;
}

Name: Anonymous 2014-08-09 7:54

>>69
*/
Nice code.

Name: Anonymous 2014-08-09 9:00

>>66
uint64_t memhash(void const* key, size_t n, uint64_t seed);
Did you mean void *const key/const void *const key? Or do you need to modify the pointed data of key?

Name: Anonymous 2014-08-12 1:43

>>71
No, I meant void const*. I'm just being idiomatic in how const modifies what precedes it (unless nothing precedes it). I read it right to left. A lot of Boost code and other third party C++ libraries are written like this.

const int* const makes my autism flare up, I much prefer int const* const. Reading right from right to left, that's constant pointer to a constant integer type.

Name: Anonymous 2014-08-12 14:41

>>71
Don't tell me you actually read that garbage.

Name: Anonymous 2014-08-12 18:53

Why is there no Io code in this thread? Io-fag, where art thou?

Name: Anonymous 2014-08-14 18:07

Delimited continuations rule. Here's a top-tier demonstration of throwing away a continuation and producing a value of a totally different (to what the cont was expecting type):

#lang racket
(require racket/control)

(print
(string-join (list "Delimit"
(reset
(+ 10 (shift k "my anus"))))))

Name: Anonymous 2014-08-14 19:52

>>75
a totally different (to what the cont was expecting type):
Fix:
a totally different (to what the cont was expecting) type:
Can't trust a lisper misplacing his parenthesis.

Name: Anonymous 2014-08-15 1:29

#lang racket
(require racket/control)

(print
(string-join (list "Delimit"
(reset
(+ 10 (shift k "my anus"))))))

Name: Anonymous 2014-08-15 1:34

#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifndef CONNECT4_H
#define CONNECT4_H
typedef struct {
char name[30];
int score;
}Player;
/* External Variables*/
extern FILE *f, *saveFile;
extern Player p[2];
extern char menuList[3][20], players[2][30], saveFileName[15];
extern int maxx, maxy, boardState[8][9], colorChoice[3],
winningPositions[2][7], curPointsPlayer[2], turn, colsFull,
popOutActive, difTime;
extern WINDOW *board, *prompt, *title;
extern time_t start_time;
/* Menu functions */
void ErrorMessage(char *s);
void Initialize();
int InitializeMenu();
void DrawMenu(int choice);
void PlayerSelect();
void DrawPickColor(int y, int colorChoice);
int Pause();
void SaveGame();
void Load();
void DrawPrompt(char *s);
void PopOutSelection();
void DrawTitle(int y);
/* Gameplay functions */
void DrawBoardLayout();
void DrawBoard();
void PrintTime();
void PrintScore();
void Play();
void PreviewPiece(int row, int colChosen, int color);
int GetAvailableRow(int col);
void AnimatePiece(int turn, int colChosen);
int CheckEndOfGameFromPosition(int row, int col);
void InitializeWinningPositions();
void BlinkWinningPositions();
void ResetBoard();
void GameIsDraw();
void PopOut(int colChosen);
void GameOver();
void PopOutSelection();
/* Score database functions */
void AddPlayer(Player p);
int SearchPlayer(Player p);
int GetPlayerScore(Player p);
void UpdatePlayer(Player p);
void PrintDatabase();
void Quit();
#endif

//game.c

#include "connect4.h"

void DrawBoardLayout() {
clear();

int c;
int x, y, boardmaxx = 44, boardmaxy = 19;
board = newwin(boardmaxy, boardmaxx, 4, 3);
wattron(board, COLOR_PAIR(3));

for(x = 0; x < boardmaxx; x++) {
mvwaddch(board, 0, x, '*');
mvwaddch(board, boardmaxy - 1, x, '*');
}

for(y = 0; y < boardmaxy; y++) {
mvwaddstr(board, y, 0, "**");
mvwaddstr(board, y, boardmaxx - 2, "**");
}

for(y = 1; y <= boardmaxy - 2; y++)
for(x = 0; x < boardmaxx; x += 6)
mvwaddstr(board, y, x, "**");

for(x = 1; x <= boardmaxx - 2; x++)
for(y = 0; y < boardmaxy; y += 3)
mvwaddch(board, y, x, '*');

refresh();
wrefresh(board);
}

void DrawBoard() {
int i, j, x, y;
for(i = 1; i <= 6; i++) {
y = 1 + 3 * (i - 1);
for(j = 1; j <= 7; j++) {
x = 2 + 6 * (j - 1);
if(boardState[i][j] != 0) {
switch(boardState[i][j]) {
case 1:
wattrset(board, COLOR_PAIR(colorChoice[1]));
break;
case 2:
wattrset(board, COLOR_PAIR(colorChoice[2]));
break;
case 3:
wattrset(board, COLOR_PAIR(8));
break;
}
mvwaddstr(board, y, x, "****");
mvwaddstr(board, y + 1, x, "****");
wattrset(board, A_NORMAL);
}
else {
wattrset(board, COLOR_PAIR(1));
mvwaddstr(board, y, x, " ");
mvwaddstr(board, y + 1, x, " ");
}
}
}
refresh();
wrefresh(board);
}

void Play() {
int c, availableRow, colChosen = 0, color = colorChoice[1];
turn = 1;
nodelay(stdscr, TRUE);
while(1) {
c = getch();
PrintTime();
PrintScore();
if(c == 'q') {
int ch;
DrawPrompt("REALLY QUIT?\n YES(y)/NO(n)");
do {
ch = getch();
}while(ch != 'y' && ch != 'n');
if(ch == 'y') {
UpdatePlayer(p[0]);
UpdatePlayer(p[1]);
Quit();
break;
}
if(ch == 'n') {
DrawBoardLayout();
DrawBoard();
}
}
if(c == 'o' && popOutActive == 1 && boardState[6][colChosen + 1] == turn) {
if(GetAvailableRow(colChosen + 1) == 0) {
colsFull--;
}
PopOut(colChosen);
DrawBoard();
turn = 3 - turn;
color = colorChoice[turn];
}
if(c == 'p') {
int diff = Pause();
start_time += diff;
}
if(c == 's') {
time_t t = time(NULL);
difTime = t - start_time;
SaveGame();
}
if(c == ' ' || c == 10) {
availableRow = GetAvailableRow(colChosen + 1);
if(availableRow > 0) {
AnimatePiece(turn, colChosen);
boardState[availableRow][colChosen + 1] = turn;
DrawBoard(boardState);
if(CheckEndOfGameFromPosition(availableRow, colChosen + 1)) {
GameOver();
}
turn = 3 - turn;
color = colorChoice[turn];
if(availableRow == 1) {
colsFull++;
if(colsFull == 7) {
colsFull = 0;
GameIsDraw();
}
}
}
}
PreviewPiece(2, colChosen, color);
if(c == KEY_LEFT || c == 'a') {
colChosen = (colChosen + 6) % 7;
PreviewPiece(2, colChosen, color);
}
if(c == KEY_RIGHT || c == 'd') {
colChosen = (colChosen + 1) % 7;
PreviewPiece(2, colChosen, color);
}
}
}
int CheckEndOfGameFromPosition(int row, int col) {
int ok = 0, count = 0, i = row, j = col;
InitializeWinningPositions();
/* check vertical */
while(boardState[i][j] == boardState[row][col] && i <= 6) {
count++;
winningPositions[0][count - 1] = i;
winningPositions[1][count - 1] = j;
i++;
}
if(count >= 4) {
return 1;
}
/* check horizontal */
count = 0; i = row; j = col;
InitializeWinningPositions();
while(boardState[i][j] == boardState[row][col] && j >= 1) {
count++;
winningPositions[0][count - 1] = i;
winningPositions[1][count - 1] = j;
j--;
}
j = col + 1;
while(boardState[i][j] == boardState[row][col] && j <= 7) {
count++;
winningPositions[0][count - 1] = i;
winningPositions[1][count - 1] = j;
j++;
}
if(count >= 4) {
return 1;
}
/* check first diagonal */
count = 0; i = row; j = col;
InitializeWinningPositions();
while(boardState[i][j] == boardState[row][col] && j <=7 && i >= 1) {
count++;
winningPositions[0][count - 1] = i;
winningPositions[1][count - 1] = j;
j++;
i--;
}
i = row + 1;
j = col - 1;
while(boardState[i][j] == boardState[row][col] && j >=1 && i <= 6) {
count++;
winningPositions[0][count - 1] = i;
winningPositions[1][count - 1] = j;
j--;
i++;
}
if(count >= 4) {
return 1;
}
/* check second diagonal */
count = 0; i = row; j = col;
InitializeWinningPositions();
while(boardState[i][j] == boardState[row][col] && j >=1 && i >= 1) {
count++;
winningPositions[0][count - 1] = i;
winningPositions[1][count - 1] = j;
j--;
i--;
}
i = row + 1;
j = col + 1;
while(boardState[i][j] == boardState[row][col] && j <= 7 && i <= 6) {
count++;
winningPositions[0][count - 1] = i;
winningPositions[1][count - 1] = j;
j++;
i++;
}
if(count >= 4) {
return 1;
}
return 0;
}
void InitializeWinningPositions() {
int i, j;
for(i = 0; i < 2; i++)
for(j = 0; j < 7; j++)
winningPositions[i][j] = 0;
}
void BlinkWinningPositions() {
int i, blinked = 0, prevValue;
while(blinked < 5) {
i = 0;
while(i < 7 && winningPositions[0][i] != 0) {
prevValue = boardState[winningPositions[0][i]][winningPositions[1][i]];
boardState[winningPositions[0][i]][winningPositions[1][i]] = 3;
i++;
}
DrawBoard(boardState);
napms(150);
i = 0;
while(i < 7 && winningPositions[0][i] != 0) {
boardState[winningPositions[0][i]][winningPositions[1][i]] = prevValue;
i++;
}
DrawBoard(boardState);
napms(120);
blinked++;
}
}
void AnimatePiece(int turn, int colChosen) {
int i = 1, availableRow = GetAvailableRow(colChosen + 1);
while(i < availableRow) {
boardState[i][colChosen + 1] = turn;
DrawBoard(boardState);
refresh();
wrefresh(board);
napms(120);
boardState[i][colChosen + 1] = 0;
DrawBoard(boardState);
refresh();
i++;
}
}

void PreviewPiece(int row, int colChosen, int color) {
int i;
for(i = 0; i < 7; i++) {
if(i == colChosen) {
attron(COLOR_PAIR(color));
mvprintw(row, 5 + 6 * i, "****");
mvprintw(row + 1, 5 + 6 * i, "****");
attroff(COLOR_PAIR(color));
}
else {
mvprintw(row, 5 + 6 * i, " ");
mvprintw(row + 1, 5 + 6 * i, " ");
}
refresh();
}
}

int GetAvailableRow(int col) {
int i = 0;
while(boardState[i + 1][col] == 0 && i <= 5)
i++;
return i;
}
/* Prints current time and time spent since the beginning of the game */
void PrintTime() {
struct tm *cur_time;
time_t t, dif;
t = time(NULL);
int hours, minutes, seconds;
cur_time = localtime(&t);
mvprintw(2, 50, "Local Time:");
mvprintw(3, 50, "%02d:%02d:%02d", cur_time -> tm_hour,
cur_time -> tm_min, cur_time -> tm_sec);
dif = t - start_time;
seconds = dif % 60;
dif= dif / 60;
minutes = dif% 60;
hours = dif / 60;
mvprintw(15, 50, "In-game time:");
mvprintw(16, 50, "%02d:%02d:%02d", hours, minutes, seconds);
}
void PrintScore() {
switch(turn) {
case 1:
mvprintw(5, 51 + strlen(p[0].name) +
strlen(" vs ") + strlen(p[1].name), " ");
attron(COLOR_PAIR(colorChoice[1]));
mvprintw(5, 48, "*");
attroff(COLOR_PAIR(colorChoice[1]));
break;
case 2:
mvprintw(5, 48, " ");
attron(COLOR_PAIR(colorChoice[2]));
mvprintw(5, 51 + strlen(p[0].name) +
strlen(" vs ") + strlen(p[1].name), "*");
attroff(COLOR_PAIR(colorChoice[2]));
break;
}
attron(A_BOLD);
mvprintw(5, 50, "%s VS %s", p[0].name, p[1].name);
attroff(A_BOLD);
/* print current score */
mvprintw(7, 50, "Score for the current session:");
mvprintw(8, 50, "%s: %d", p[0].name, curPointsPlayer[0]);
mvprintw(9, 50, "%s: %d", p[1].name, curPointsPlayer[1]);
/* print total score for each player */
mvprintw(11, 50, "Lifetime Player Score:");
mvprintw(12, 50, "%s: %d", p[0].name, p[0].score);
mvprintw(13, 50, "%s: %d", p[1].name, p[1].score);
if(popOutActive == 1) {
mvprintw(18, 50, "POPOUT: O");
}
else {
mvprintw(18, 50, "Default Key bindings:");
}
mvprintw(19, 50, "LEFT: A | LEFT: <-");
mvprintw(20, 50, "RIGHT: D | RIGHT: ->");
mvprintw(21, 50, "_____________________________");
mvprintw(22, 50, "ACTION: SPACE | ACTION: ENTER");
mvprintw(23, 50, "SAVE:S | QUIT:Q | PAUSE:P");
}
/* Put zeroes in the boardState matrix */
void ResetBoard() {
int i, j;
for(i = 0; i < 8; i++)
for(j = 0; j < 9; j++)
boardState[i][j] = 0;
}
void GameIsDraw() {
char *msg = "DRAW!\n PLAY AGAIN?\n YES(y) / NO(n)";
int ch;
DrawPrompt(msg);
do {
ch = getch();
}while(ch != 'y' && ch != 'n');
if(ch == 'n') {
UpdatePlayer(p[0]);
UpdatePlayer(p[1]);
Quit();
endwin();
exit(0);
}
if(ch == 'y') {
ResetBoard();
DrawBoardLayout();
DrawBoard();
}
}
void PopOut(int colChosen) {
int i, winningCombinations[2] = {0};
for(i = 6; i >= 1; i--) {
if(boardState[i][colChosen + 1] != 0) {
boardState[i][colChosen + 1] = 0;
DrawBoard();
napms(180);
boardState[i][colChosen + 1] = boardState[i - 1][colChosen + 1];
}
}
for(i = 6; i >= 1; i--) {
if(boardState[i][colChosen + 1] != 0) {
if(CheckEndOfGameFromPosition(i, colChosen + 1)) {
BlinkWinningPositions();
winningCombinations[boardState[i][colChosen + 1] - 1]++;
}
}
}
if(winningCombinations[0] > 0 && winningCombinations[1] > 0) {
GameIsDraw();
}
else
for(i = 0; i < 2; i++) {
if(winningCombinations[i] > 0) {
char msg[100];
int ch;
colsFull = 0;
sprintf(msg, "%s WINS!\n PLAY AGAIN?\n YES(y)/NO(n)",
p[i].name);
curPointsPlayer[i]++;
p[i].score++;
PrintScore();
DrawPrompt(msg);
while((ch = getch()) != 'y' && ch != 'n');
if(ch == 'n') {
UpdatePlayer(p[0]);
UpdatePlayer(p[1]);
Quit();
endwin();
exit(0);
}
if(ch == 'y') {
ResetBoard();
DrawBoardLayout();
DrawBoard();
}
}
}
}
/* Update variables and print message when the game is over */
void GameOver() {
char msg[100];
int ch;
colsFull = 0;
sprintf(msg, "%s WINS!\n PLAY AGAIN OR EXIT?\n YES(y)/NO(n)",
p[turn - 1].name);
curPointsPlayer[turn - 1]++;
p[turn - 1].score++;
PrintScore();
BlinkWinningPositions();
DrawPrompt(msg);
while((ch = getch()) != 'y' && ch != 'n');
if(ch == 'n') {
UpdatePlayer(p[0]);
UpdatePlayer(p[1]);
Quit();
endwin();
exit(0);
}
if(ch == 'y') {
ResetBoard();
DrawBoardLayout();
DrawBoard();
}
}

Name: Anonymous 2014-08-15 1:49

>>78
I wanted to write a connect 4 after I read about your implementation here on /prog/. Now I see that you post it :) Nice. I will hopefully read your code and discover bugs, if any. Thanks.

Name: Anonymous 2014-08-15 16:50

//menu
#include "connect4.h"

void ErrorMessage(char *s) {
addstr(s);
refresh();
getch();
endwin();
exit(1);
}

void Initialize() {
initscr();
cbreak();
noecho();
curs_set(0);
keypad(stdscr, TRUE);
start_color();
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_MAGENTA, COLOR_MAGENTA);
init_pair(5, COLOR_RED, COLOR_RED);
init_pair(6, COLOR_GREEN, COLOR_GREEN);
init_pair(7, COLOR_BLUE, COLOR_BLUE);
init_pair(8, COLOR_WHITE, COLOR_WHITE);
init_pair(9, COLOR_CYAN, COLOR_CYAN);
}

int InitializeMenu() {
int c, i = 0;
int choice = 0;
char *s = "PRESS ENTER/SPACE TO SELECT OPTION";
nodelay(stdscr, TRUE);
DrawMenu(choice);
DrawTitle(0);
while(1) {
refresh();
wrefresh(title);

c = getch();
if(c == 10 || c == ' ')
break;
if(c == KEY_DOWN) {
choice = (choice + 1) % 3;
DrawMenu(choice);
}
if(c == KEY_UP) {
choice = (choice + 2) % 3;
DrawMenu(choice);
}

if(i < strlen(s)) {
mvaddstr(maxy - 1, maxx - 1 - i, s);
napms(60);
i++;
}
refresh();
}
return choice;
}

void DrawMenu(int choice) {
int i;
for(i = 0; i < 3; i++) {
move(maxy / 2 + 2 * (i - 1), (maxx - strlen(menuList[1])) / 2 );
if(i == choice) {
attron(A_REVERSE);
printw("%s", menuList[i]);
attroff(A_REVERSE);
}
else
printw("%s", menuList[i]);
}
}

/* Select name and color for both players */
void PlayerSelect() {
char *msg1 = "CHOOSE COLOR, ";
int c, i;
nodelay(stdscr, FALSE);
clear();
echo();
mvprintw(maxy / 4, maxx / 6, "ENTER P1 NAME: ");
refresh();
getnstr(p[0].name, 10);
mvprintw(maxy / 4 + 2, maxx / 6, "ENTER P2 NAME: ");
getnstr(p[1].name, 10);

/* Check if player is in database */
for(i = 0; i <= 1; i++) {
if(SearchPlayer(p[i]))
p[i].score = GetPlayerScore(p[i]);
else {
p[i].score = 0;
AddPlayer(p[i]);
}
}
clear();
noecho();

/* Print Color Choice Menu for Player 1 */
mvprintw(1, (maxx - strlen(msg1) - strlen(p[0].name)) / 2,
"%s%s", msg1, p[0].name);
DrawPickColor(3, colorChoice[1]);
while(1) {
c = getch();
if(c == ' ' || c == 10)
break;
if(c == KEY_LEFT) {
colorChoice[1] = (colorChoice[1] + 2 ) % 3;
DrawPickColor(3, colorChoice[1]);
}
if(c == KEY_RIGHT) {
colorChoice[1] = (colorChoice[1] + 1) % 3;
DrawPickColor(3, colorChoice[1]);
}
refresh();
}

/* Print Color Choice Menu for Player 2 */
mvprintw(6, (maxx - strlen(msg1) - strlen(p[1].name)) / 2,
"%s%s", msg1, p[1].name);
DrawPickColor(8, colorChoice[2]);
while(1) {
c = getch();
if(c == ' ' || c == 10) {
if(colorChoice[2] == colorChoice[1]) {
char msg[100];
sprintf(msg, "%s are you sure you want \
to play with the same color as %s?", p[1].name, p[0].name);
mvprintw(14, (maxx - strlen(msg)) / 2, "%s", msg);
mvprintw(15, (maxx - strlen("YES(y) / NO(n)")) / 2, "YES(y) / NO(n)");
int ch;
do {
ch = getch();
}while(ch != 'y' && ch != 'n');

if(ch == 'y')
break;
else {
DrawPickColor(8, colorChoice[2]);
mvprintw(14, 0, " \
");
mvprintw(15, (maxx - strlen("YES(y) / NO(n)")) / 2, " ");
}
}
else
break;
}

if(c == KEY_LEFT) {
colorChoice[2] = (colorChoice[2] + 2 ) % 3;
DrawPickColor(8, colorChoice[2]);
}
if(c == KEY_RIGHT) {
colorChoice[2] = (colorChoice[2] + 1) % 3;
DrawPickColor(8, colorChoice[2]);
}
refresh();
}

/* Increase colorChoice so that would match the COLOR_PAIR */
colorChoice[1] += 5;
colorChoice[2] += 5;
}

void DrawPickColor(int y, int colorChoice) {
int i;
switch(colorChoice) {
case 0:
mvaddch(y, 6, '*');
mvaddch(y, maxx / 2 - 2, ' ');
mvaddch(y, maxx - 13, ' ');
break;
case 1:
mvaddch(y, 6, ' ');
mvaddch(y, maxx / 2 - 2, '*');
mvaddch(y, maxx - 13, ' ');
break;
case 2:
mvaddch(y, 6, ' ');
mvaddch(y, maxx / 2 - 2, ' ');
mvaddch(y, maxx - 13, '*');
break;
}
attrset(COLOR_PAIR(1));
mvprintw(y, 7, "RED");
attrset(COLOR_PAIR(2));
mvprintw(y, maxx / 2 - 1, "GREEN");
attrset(COLOR_PAIR(3));
mvprintw(y, maxx - 12, "BLUE");
attrset(A_NORMAL);
}


void Quit() {
clear();
char *msg = "EXIT";
mvaddstr(maxy / 2, (maxx - strlen(msg)) / 2, msg);
DrawTitle(0);
refresh();
wrefresh(title);
napms(1500);
}

int Pause() {
int c;
time_t start_pause = time(NULL), end_pause;
char *msg = "GAME PAUSED ---> PRESS P TO RESUME",
*msg2 = " ";
mvprintw(0, (maxx - strlen(msg)) / 2, "%s", msg);
while(1) {
c = getch();
if(c == 'p') {
end_pause = time(NULL);
mvprintw(0, (maxx - strlen(msg2)) / 2, "%s", msg2);
break;
}
}
int diff = end_pause - start_pause;
return diff;
}

void SaveGame() {
clear();
nodelay(stdscr, FALSE);
echo();
mvprintw(4, 4, "Insert the name for save file: ");

DrawTitle(14);

getnstr(saveFileName, 10);
saveFile = fopen(saveFileName, "ab");
if(saveFile == NULL)
ErrorMessage("Error at opening save file!");
fwrite(p, sizeof(Player), 2, saveFile);
fwrite(curPointsPlayer, sizeof(curPointsPlayer[1]), 2, saveFile);
fwrite(boardState, sizeof(boardState), 1, saveFile);
fwrite(colorChoice, sizeof(colorChoice), 1, saveFile);
fwrite(&colsFull, sizeof(colsFull), 1, saveFile);
fwrite(&difTime, sizeof(int), 1, saveFile);
fwrite(&popOutActive, sizeof(int), 1, saveFile);
noecho();
nodelay(stdscr, TRUE);
DrawBoardLayout();
DrawBoard();
fclose(saveFile);
}

void Load() {
clear();
nodelay(stdscr, FALSE);
echo();
mvprintw(4, 4, "Insert the name for save file: ");
getnstr(saveFileName, 10);
noecho();
saveFile = fopen(saveFileName, "rb");
if(saveFile == NULL)
ErrorMessage("Error at opening save file!");

fread(&p[0], sizeof(Player), 1, saveFile);
fread(&p[1], sizeof(Player), 1, saveFile);
fread(&curPointsPlayer[0], sizeof(curPointsPlayer[0]), 1, saveFile);
fread(&curPointsPlayer[1], sizeof(curPointsPlayer[1]), 1, saveFile);
fread(boardState, sizeof(boardState), 1, saveFile);
fread(colorChoice, sizeof(colorChoice), 1, saveFile);
fread(&colsFull, sizeof(colsFull), 1, saveFile);
fread(&difTime, sizeof(int), 1, saveFile);
fread(&popOutActive, sizeof(int), 1, saveFile);

init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_MAGENTA, COLOR_MAGENTA);
init_pair(5, COLOR_RED, COLOR_RED);
init_pair(6, COLOR_GREEN, COLOR_GREEN);
init_pair(7, COLOR_BLUE, COLOR_BLUE);
init_pair(8, COLOR_WHITE, COLOR_WHITE);
fclose(saveFile);
start_time = time(NULL);
start_time = start_time - difTime;
DrawBoardLayout();
DrawBoard();
Play();
}

void DrawPrompt(char *s) {
int x, y;
prompt = newwin(5, 40, 5, 5);
getmaxyx(prompt, y, x);
mvwprintw(prompt, 1, 1, "%s", s);
refresh();
touchwin(prompt);
wrefresh(prompt);
getch();
}

void PopOutSelection() {
clear();
mvprintw(1, (maxx - strlen("Choose a gamestyle")) / 2, "Choose a gamestyle");
int c;
char options[2][30] = {"Normal", "Pop Out"};
nodelay(stdscr, FALSE);
attron(A_REVERSE);
mvprintw(5, (maxx - strlen(options[1])) / 2, options[0]);
attroff(A_REVERSE);
mvprintw(7, (maxx - strlen(options[1])) / 2, options[1]);

DrawTitle(11);

while(1) {
refresh();
wrefresh(title);
c = getch();
if(c == ' ' || c == 10) {
break;
}

if(c == KEY_DOWN || c == KEY_UP) {
popOutActive = (popOutActive + 1) % 2;
if(popOutActive == 0) {
attron(A_REVERSE);
mvprintw(5, (maxx - strlen(options[1])) / 2, options[0]);
attroff(A_REVERSE);
mvprintw(7, (maxx - strlen(options[1])) / 2, options[1]);
}
else {
mvprintw(5, (maxx - strlen(options[1])) / 2, options[0]);
attron(A_REVERSE);
mvprintw(7, (maxx - strlen(options[1])) / 2, options[1]);
attroff(A_REVERSE);
}
}
}
}

void DrawTitle(int y) {
title = newwin(7, 79, y, 0);
wattron(title, COLOR_PAIR(3));
int i;
for(i = 0; i < 5; i++) {
refresh();
wclear(title);
/* First Row */
mvwprintw(title, i - 4, 1, "********");
mvwprintw(title, i - 4, 11, "********");
mvwprintw(title, i - 4, 21, "********");
mvwprintw(title, i - 4, 31, "********");
mvwprintw(title, i - 4, 41, "********");
mvwprintw(title, i - 4, 51, "********");
mvwprintw(title, i - 4, 61, "********");
mvwprintw(title, i - 4, 71, "*");
mvwprintw(title, i - 4, 78, "*");

/* Second Row */
/* C */
mvwprintw(title, i - 3, 1, "*");
/* O */
mvwprintw(title, i - 3, 11, "*");
mvwprintw(title, i - 3, 18, "*");
/* N */
mvwprintw(title, i - 3, 21, "*");
mvwprintw(title, i - 3, 28, "*");
/* N */
mvwprintw(title, i - 3, 31, "*");
mvwprintw(title, i - 3, 38, "*");
/* E */
mvwprintw(title, i - 3, 41, "*");
/* C */
mvwprintw(title, i - 3, 51, "*");
/* T */
mvwprintw(title, i - 3, 65, "*");
/* 4 */
mvwprintw(title, i - 3, 71, "*");
mvwprintw(title, i - 3, 78, "*");

/* Third Row */
/* C */
mvwprintw(title, i - 2, 1, "*");
/* O */
mvwprintw(title, i - 2, 11, "*");
mvwprintw(title, i - 2, 18, "*");
/* N */
mvwprintw(title, i - 2, 21, "*");
mvwprintw(title, i - 2, 28, "*");
/* N */
mvwprintw(title, i - 2, 31, "*");
mvwprintw(title, i - 2, 38, "*");
/* E */
mvwprintw(title, i - 2, 41, "********");
/* C */
mvwprintw(title, i - 2, 51, "*");
/* T */
mvwprintw(title, i - 2, 65, "*");
/* 4 */
mvwprintw(title, i - 2, 71, "********");

/* Fourth Row */
/* C */
mvwprintw(title, i - 1, 1, "*");
/* O */
mvwprintw(title, i - 1, 11, "*");
mvwprintw(title, i - 1, 18, "*");
/* N */
mvwprintw(title, i - 1, 21, "*");
mvwprintw(title, i - 1, 28, "*");
/* N */
mvwprintw(title, i - 1, 31, "*");
mvwprintw(title, i - 1, 38, "*");
/* E */
mvwprintw(title, i - 1, 41, "*");
/* C */
mvwprintw(title, i - 1, 51, "*");
/* T */
mvwprintw(title, i - 1, 65, "*");
/* 4 */
mvwprintw(title, i - 1, 78, "*");

/* Fifth Row */
/* C */
mvwprintw(title, i, 1, "********");
/* O */
mvwprintw(title, i, 11, "********");
/* N */
mvwprintw(title, i, 21, "*");
mvwprintw(title, i, 28, "*");
/* N */
mvwprintw(title, i, 31, "*");
mvwprintw(title, i, 38, "*");
/* E */
mvwprintw(title, i, 41, "********");
/* C */
mvwprintw(title, i, 51, "********");
/* T */
mvwprintw(title, i, 65, "*");
/* 4 */
mvwprintw(title, i, 78, "*");
napms(150);
refresh();
wrefresh(title);
}

wattroff(title, COLOR_PAIR(10));
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List