Quantcast
Channel: Forum Pasja Informatyki - Najnowsze pytania bez odpowiedzi
Viewing all articles
Browse latest Browse all 21942

Code Review - TicTacToe

$
0
0
Powiecie mi o wszystkich złych nawykach jakie tu zastosowałem? Chciałbym żebyście nie pozostawili po mnie suchej nitki. Wiem na pewno że pierwszy błąd to zmienne globalne tylko nie bardzo wiem jak ich w modułach bo inne funkcje ich nie widzą. 



var turaa = 1; 
var znak = "";
var znak1= "X";
var znak2 = "O";
var licznik = 0;

//wypisywanie pol X lub O, turowosc-----------------------------------------------------------------------
function podmien(nr)
{
	if(turaa == 1) {
		znak = znak1;
		turaa--;
		document.getElementById("tura").innerHTML = "Tura gracza O"
	}
 else  if(turaa == 0)
	{
		znak = znak2;
		turaa++;
		document.getElementById("tura").innerHTML = "Tura gracza X"
	}
	var myid = "pole" + nr;
	document.getElementById(myid).innerHTML = znak;
	document.getElementById(myid).setAttribute("onclick",";");
	document.getElementById(myid).style.cursor = "default";	
	document.getElementById(myid).onmouseover = function() {
    this.style.backgroundColor = "coral";}
	licznik++;
	sprawdz();
}

//sprawdzanie wygranej----------------------------------------------------------------------------------
function sprawdz()
{
	var uchwyt = document.getElementsByClassName("box");
	
var p1 = uchwyt[0].innerHTML;   var p2 = uchwyt[1].innerHTML;   var p3 = uchwyt[2].innerHTML;
var p4 = uchwyt[3].innerHTML;   var p5 = uchwyt[4].innerHTML;   var p6 = uchwyt[5].innerHTML;
var p7 = uchwyt[6].innerHTML;   var p8 = uchwyt[7].innerHTML;   var p9 = uchwyt[8].innerHTML;

//sprawzdanie wygranej dla O
	if((p1==znak2&&p2==znak2&&p3==znak2)||(p4==znak2&&p5==znak2&&p6==znak2)||(p7==znak2&&p8==znak2&&p9==znak2)||(p1==znak2&&p4==znak2&&p7==znak2)||(p2==znak2&&p5==znak2&&p8==znak2)||(p3==znak2&&p6==znak2&&p9==znak2)||(p1==znak2&&p5==znak2&&p9==znak2)||(p3==znak2&&p5==znak2&&p7==znak2))
	{
		document.getElementById("wygrana").innerHTML = "<a href=\"index.html\">Wygrał O! Jeszcze raz?</a>";
		koniec();
	}
//sprawzdanie wygranej dla X
	if((p1==znak1&&p2==znak1&&p3==znak1)||(p4==znak1&&p5==znak1&&p6==znak1)||(p7==znak1&&p8==znak1&&p9==znak1)||(p1==znak1&&p4==znak1&&p7==znak1)||(p2==znak1&&p5==znak1&&p8==znak1)||(p3==znak1&&p6==znak1&&p9==znak1)||(p1==znak1&&p5==znak1&&p9==znak1)||(p3==znak1&&p5==znak1&&p7==znak1))
	{
		document.getElementById("wygrana").innerHTML = "<a href=\"index.html\">Wygrał X! Jeszcze raz?</a>";
		koniec();
	}
//sprawdzanie remisu---------------------------------------------------------------------------------------
	if(licznik == 9) 
		{
		document.getElementById("wygrana").innerHTML = "<a href=\"index.html\">Remis! Jeszcze raz?</a>";
		koniec();
	}
}
//co ma sie stac po wygranej------------------------------------------------------------------------
function koniec()
{
		document.getElementById("pole1").setAttribute("onclick",";");
		document.getElementById("pole2").setAttribute("onclick",";");
		document.getElementById("pole3").setAttribute("onclick",";");
		document.getElementById("pole4").setAttribute("onclick",";");
		document.getElementById("pole5").setAttribute("onclick",";");
		document.getElementById("pole6").setAttribute("onclick",";");
		document.getElementById("pole7").setAttribute("onclick",";");
		document.getElementById("pole8").setAttribute("onclick",";");
		document.getElementById("pole9").setAttribute("onclick",";");
		
		document.getElementById("pole1").onmouseover = function() {
		this.style.backgroundColor = "coral";}
		document.getElementById("pole2").onmouseover = function() {
		this.style.backgroundColor = "coral";}
		document.getElementById("pole3").onmouseover = function() {
		this.style.backgroundColor = "coral";}
		document.getElementById("pole4").onmouseover = function() {
		this.style.backgroundColor = "coral";}
		document.getElementById("pole5").onmouseover = function() {
		this.style.backgroundColor = "coral";}
		document.getElementById("pole6").onmouseover = function() {
		this.style.backgroundColor = "coral";}
		document.getElementById("pole7").onmouseover = function() {
		this.style.backgroundColor = "coral";}
		document.getElementById("pole8").onmouseover = function() {
		this.style.backgroundColor = "coral";}
		document.getElementById("pole9").onmouseover = function() {
		this.style.backgroundColor = "coral";}
		
		document.getElementById("pole1").style.cursor = "default";
		document.getElementById("pole2").style.cursor = "default";
		document.getElementById("pole3").style.cursor = "default";
		document.getElementById("pole4").style.cursor = "default";
		document.getElementById("pole5").style.cursor = "default";
		document.getElementById("pole6").style.cursor = "default";
		document.getElementById("pole7").style.cursor = "default";
		document.getElementById("pole8").style.cursor = "default";
		document.getElementById("pole9").style.cursor = "default";
}

 


Viewing all articles
Browse latest Browse all 21942

Trending Articles


TRX Antek AVT - 2310 ver 2,0


Автовишка HAULOTTE HA 16 SPX


POTANIACZ


Zrób Sam - rocznik 1985 [PDF] [PL]


Maxgear opinie


BMW E61 2.5d błąd 43E2 - klapa gasząca a DPF


Eveline ➤ Matowe pomadki Velvet Matt Lipstick 500, 506, 5007


Auta / Cars (2006) PLDUB.BRRip.480p.XviD.AC3-LTN / DUBBING PL


Peugeot 508 problem z elektroniką


AŚ Jelenia Góra