Skip to content

Instantly share code, notes, and snippets.

@josejuan
Created June 16, 2012 23:00
Show Gist options
  • Save josejuan/2942722 to your computer and use it in GitHub Desktop.
Save josejuan/2942722 to your computer and use it in GitHub Desktop.
Check 3 en raya
typedef struct _Partida {
unsigned short a:1;
unsigned short b:1;
unsigned short c:1;
unsigned short d:1;
unsigned short e:1;
unsigned short f:1;
unsigned short g:1;
unsigned short h:1;
unsigned short i:1;
} Partida;
// ¡Esta es la evidente, pero tiene 7 saltos condicionales!
int Check_lento(Partida p) {
return (p.a&&p.b&&p.c)||(p.d&&p.e&&p.f)||(p.g&&p.h&&p.i)||
(p.a&&p.d&&p.g)||(p.b&&p.e&&p.h)||(p.c&&p.f&&p.i)||
(p.a&&p.e&&p.i)||(p.g&&p.e&&p.c);
}
// Se puede hacer mucho más rápida la evaluación sin saltos:
int Check_rapido(Partida p) {
return (p.a&p.b&p.c)|(p.d&p.e&p.f)|(p.g&p.h&p.i)|
(p.a&p.d&p.g)|(p.b&p.e&p.h)|(p.c&p.f&p.i)|
(p.a&p.e&p.i)|(p.g&p.e&p.c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment